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

jrean's avatar

Session issue

Hi,

I have a very weir behaviour with my fresh L5.1 install.

On a test route /test I have the following:

    // No user authenticated
        dump(\Auth::check()); // false

    // Fetch a User
        $user = \App\User::first();

    // User
        dump($user->toArray());

    // Still no authenticated User (expected)
        dump(\Auth::check()); // false

    // Log User in
        \Auth::login($user);

    // Check if User is logged in
        dump(\Auth::check()); // true

    // Debug
        dump(\Session::all());

    // Debug
        dump(\Request::cookie('laravel_session'));

So this will fetch the User, this will log the User and this will output the following:

false

array:5 [
  "id" => "2"
  "name" => "XX XX"
  "email" => "xx@xx.com"
  "created_at" => "2015-06-27 18:42:02"
  "updated_at" => "2015-06-28 10:52:38"
]

false

true

array:4 [▼
  "_token" => "miFxISw21eNXBxh2GkxmGc8aNZHDBDpoONPCTVch"
  "_previous" => array:1 [▶]
  "flash" => array:2 [▶]
  "login_82e5d2c56bdd0811318f0cf078b78bfc" => "2"
]

"cc51a357d70075cb1ea159747cbef7bbd1a01964"

NOW I try to reload the exact same page BUT I comment out the everything except:

    // Debug
        dump(\Session::all());

    // Debug
        dump(\Request::cookie('laravel_session'));

Here is the output:

array:1 [▼
  "_token" => "erHWVBpba7uM058PZsx4VKMQAWHaytYeormUZGZG"
]

"cc51a357d70075cb1ea159747cbef7bbd1a01964"

No User in session! ...

My env('SESSION_DRIVER') is set to file.

Any idea?

0 likes
3 replies
milon's avatar

what is your file permission on storage directory?

jrean's avatar

I use homestead so the storage file permission shouldn't be a problem. drwxr-xr-x 1 vagrant vagrant 204 Jun 28 02:52 storage/

jrean's avatar
jrean
OP
Best Answer
Level 8

I found. When using \Auth::attempt() and \Auth::login() we NEED to return a redirect(). This is only after that redirect the session will be stored. I can now access the authenticated user with \Auth::user() and/or test it with \Auth::check().

Please or to participate in this conversation.