Auth::guest() returns true for both logged in and logged out users

rranj picture rranj · Jan 16, 2016 · Viewed 14.3k times · Source

I am new to laravel and so I was trying to create a small project for learning purpose. I have used the inbuilt Auth for login and register. These automatically generated pages work so well, now In this I created a route to resource posts by using controller called postcontroller.

Now in the postcontroller I check if the user is authorized to return a view: posts else to login page. So to check it I do following

if (!Auth::guest())

return view('posts');

else

return "......";

Now here the Auth::guest() returns true for both logged in and logged out users.

Answer

Rafael Ferreira picture Rafael Ferreira · Jan 16, 2016

Are you sure that you are logged in?

try to dump your Auth::user() data with

dd(Auth::user());

And by the way, if you are returning in the if statement you do not need to use else.