16

Laravel framework- Why aren't I able to use Auth::user(), (to see if user has been logged in) in the controller of the laravel project. Is the Session not connected to the controller?

HomeController.php

 public function isauthorized(){
    if (Auth::user()){
        return View::make('home.basic')
            ->with('basic1', 'Authorized');
    }else{
        return View::make('home.basic')
            ->with('basic1', 'Not Authorized');
    }
}

My login system is working and the session is always set when logged in. When I call the function Auth::user() in the View or in routes.php, it is succesfull. But when I call the function in the controller, it is false. Help please.

2
  • 1
    Simple debugging. Use dd(Auth::user()); before your if. Commented Jul 30, 2014 at 6:56
  • Make sure that the app/storage directory is writable by the Web Server Commented Jul 30, 2014 at 17:13

10 Answers 10

23

The problem is that you haven't included the Auth facade.

In your HomeController.php do:

use Illuminate\Support\Facades\Auth;
Sign up to request clarification or add additional context in comments.

1 Comment

auth()->user() without any use
8

Just add the following in your controller library.

use Illuminate\Support\Facades\Auth;

1 Comment

This is the same solution provided by Zaerdna' answer earlier this year. When you answer a question, be sure you add value on top of the existing answers. Thanks!
7

If you're on Laravel 5.x, you'll probably have the auth middleware component. That might be the missing link.

I often use a pattern like this in my controllers:

public function __construct()
    {
        $this->middleware('auth');
        $this->user =  \Auth::user();
    }

then you can

if ($this->user .. )

or

if ($this->user->can('something')) with Entrust or similar

Comments

5

To see if a user is loged in don't use Auth::user(). Use Auth::check() instead.

if (Auth::check()){
    return View::make('home.basic')->with('user', Auth::user());
}

1 Comment

Why would Auth::check() be better? I quote: In other words, Auth::check() calls Auth::user(), gets the result from it, and then checks to see if the user exists. The main difference is that it checks if the user is null for you so that you get a boolean value. stackoverflow.com/a/33483798/460885
4

You need to use the Auth facade in the controller;

add this line just before class definition.

use Auth;

Comments

3

in your controllers constructor:

public function __construct(){
    $this->middleware('auth');
}

Comments

2

I have the same problem. The solution I've found, is to not use \Auth::user() (it returns null). Instead, go through the guard like this: \Auth::guard('mrm')->User(). Obviously replace 'mrm' with whatever auth guard you are using.

This works, correctly returning the current user even when Auth::User returns null, and you can also use other methods on it - e.g. \Auth::guard('mrm')->check() instead of \Auth::check() (which always returns false).

No idea why the Auth facade doesn't work, but at least there is an alternative that does.

Comments

1

additionally, you can also use the auth() helper without adding the facade in your controller. (i.e. auth()->user()

Comments

0

Use Auth::check()function to check if user is logged or not

Comments

0
namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    public function __construct()
    {
        $this->middleware('web');
    }

    public function isAuthorized()
    {
        if (Auth::user()) {
            return view('home.basic')->with('basic1', 'Authorized');
        } else {
            return view('home.basic')->with('basic1', 'Not Authorized');
        }
    }
}

1 Comment

Welcome to SO! Please add an explanation to this answer to make it useful and so it is up to standard. stackoverflow.com/help/how-to-answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.