2

I have followed the manual because I would like to use the remember_me token with Laravel. To verify it works I have a test middleware that do:

Auth::loginUsingId(1, true);
return $next($request);

Later, on my test view I have:

{{@if (Auth::check())}}
Yeah!
{{@endif}}

So yes I am authenticated, but no I do not have any remember_me cookie set. I only see the session cookie.

I have also tried to use Auth::User()->setRememberToken('foobar'), but I got the same result: no remember_me cookie.

As advised from this question, I have set my session settings to:

'lifetime' => 10080,
'expire_on_close' => true,

From this other question I verified that AuthenticateSession middleware is enabled:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middlewares\Test::class, // Auth user 1 by id
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
    ],

Of course if I do the following I get false:

dd(Auth::viaRemember());

Some other similar questions advise to use the cookies for the session. My config('session') looks like:

array:15 [▼
  "driver" => "cookie"
  "lifetime" => 10080
  "expire_on_close" => true
  "encrypt" => false
  "connection" => null
  "table" => "sessions"
  "store" => null
  "cookie" => "laravel_session"
  "path" => "/"
  "domain" => null
  "secure" => false
  "http_only" => true
  "same_site" => null
]
nowox
  • 25,978
  • 39
  • 143
  • 293
  • What about putting the Test Middleware after the StartSession or after both of the two middlewares? – mdexp Jun 27 '19 at 09:11

1 Answers1

2

Change session driver to use cookie Like:

.env

SESSION_DRIVER=cookie

OR

app/config/session.php

'driver' => 'cookie',

Also changed the order of middleware like:

'web' => [ 

    \Illuminate\Session\Middleware\StartSession::class, 
    \Illuminate\Session\Middleware\AuthenticateSession::class,
    \App\Http\Middlewares\Test::class, // Auth user 1 by id 
],

And last but not least, verify you have the middleware AddQueuedCookiesToResponse enabled.

nowox
  • 25,978
  • 39
  • 143
  • 293
Vikash Pathak
  • 3,444
  • 1
  • 18
  • 32