Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

andreamanini's avatar

Laravel auth()->check or auth()->user logs users out

I am using laravel 5.4.36 and during the development I noticed that Laravel can't persist users in the session. I've also tried by switching to database session but the same problem occurs.

Any idea why ? Where should I check ?

0 likes
8 replies
tisuchi's avatar

Try this-

if(Auth::check()){
    Auth::logout(); 
}

For checking whether this user is logged in or not, normally we use-

Auth::check()

It returns true or false.

4 likes
andreamanini's avatar

mmmh I'm not too sure what you mean by that!

What I'm saying is that whenever I use Auth::check() the user already gets logged out..

I have a login form, then some blade templates where I use auth()->check() to test if the user may see some part of the view like some menu elements and so on.

When I log in, everything works fine but the check on the user doesn't seem to be working correctly

tisuchi's avatar

I see...

You may be use Session::flush(); to clear all of your existing session first and try again.

2 likes
andreamanini's avatar

Nope, that doesn't work either.

I've tried different browsers, cleared the cache (both browser and with php artisan), tried to check the user in a slightly different way ( by using ! empty(auth()->user()) ) but nothing seem to be working :(

This is my Auth/LoginController

   use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * LoginController constructor.
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

This is one of the pages where I directly check if the user is logged in, and instead of letting me through (after the login) it redirects me to the login page again.

   /**
     * User's favourite page
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function usersFavourite()
    {
        if (false == Auth::check()) {
            return redirect('/login');
        }

        $favouriteRecipes = auth()->user()->favourites('recipes', null, true);
        $favouriteIngredients = auth()->user()->favourites('ingredients', null, true);

        return view('desk.favourites', compact('favouriteRecipes', 'favouriteIngredients'));
    }

It was working up until the other day and I can't spot what was the change that made it not work

andreamanini's avatar

here it is

Auth::routes();

Route::get('/', 'DeskController@index');
Route::get('/ingredienti', 'DeskController@ingredients');
Route::get('/ricette', 'DeskController@recipes');
Route::get('/utente/{user}', 'DeskController@userProfile');
Route::get('/ingrediente/{ingrediente}', 'DeskController@ingredientDetail');
Route::get('/blogger/{blogger}', 'DeskController@bloggerProfile');
Route::get('/ricetta/{ricetta}', 'DeskController@recipeDetail');
Route::get('/preferiti', 'DeskController@usersFavourite');

Route::get('/home', 'HomeController@index')->name('home');


// Facebook login
Route::get('auth/facebook', 'Auth\RegisterController@redirectToProvider');
Route::get('auth/facebook/callback', 'Auth\RegisterController@handleProviderCallback');
adamprickett's avatar

Check that session cookies are being set correctly.

First place to check the path, domain and secure keys in your /config/session.php. Make sure these match your current environment.

andreamanini's avatar

I believe I found what the error was!

basically in the menu I had a link for logout which was

<a href="{{ auth()->logout()">logout</a>

I think this was the main problem of my authentication issues .... I feel so ...

1 like

Please or to participate in this conversation.