Leo
Leo

Reputation: 7420

Auth::check() returns false Laravel 5.4

I have created the routes and views needed for authentication using one simple command:

php artisan make:auth

Everything works fine login and register sections. However when I go to my controller's constructor to check if the user its logged in I always get false response; even though the user its logged in!

public function __construct()
{
    dd(Auth::check());

}

Any idea?! And yes I did use Auth; at the top.

Upvotes: 4

Views: 4952

Answers (2)

Ru Chern Chong
Ru Chern Chong

Reputation: 3756

Use the helper function auth()->check() and add

$this->middleware('auth') to the function __construct() method.

Upvotes: 1

Conor Mancone
Conor Mancone

Reputation: 2030

Middleware (and therefore setting the logged in user) don't happen until after the controller constructor. See this related question/answer for more details:

Laravel 5 Auth is non object in Controller construct

Upvotes: 2

Related Questions