php - 记住不使用 Laravel 的用户

标签 php laravel authentication cookies remember-me

我已关注manual因为 我想在 Laravel 中使用 remember_me token 。为了验证它的工作原理,我有一个测试中间件,它可以:

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

后来,在我的测试 View 中,我有:

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

所以,是的,我已经通过了身份验证,但没有,我没有设置任何 remember_me cookie。我只看到 session cookie。

我也尝试过使用Auth::User()->setRememberToken('foobar'),但我得到了相同的结果:没有remember_me cookie。

根据 this question 的建议,我已将 session 设置设置为:

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

来自此另一个 question我验证了 AuthenticateSession 中间件已启用:

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

当然,如果我执行以下操作,我会得到false:

dd(Auth::viaRemember());

其他一些类似的问题建议在 session 中使用cookie。我的 config('session') 如下所示:

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
]

最佳答案

更改 session 驱动程序以使用cookie,例如:

.env

SESSION_DRIVER=cookie

或者

app/config/session.php

'driver' => 'cookie',

还更改了中间件的顺序,例如:

'web' => [ 

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

最后但并非最不重要的一点是,验证您是否已启用中间件 AddQueuedCookiesToResponse

关于php - 记住不使用 Laravel 的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56787406/

相关文章:

php - Laravel Dusk(Windows 平台不支持 TTY 模式)

php - ftp_put() : Command STOR failed

authentication - 用户 "system"无法列出集群中的所有服务

php - 许多BETWEEN和AND错误答案mysql

php - 寻找基于PHP的用户管理系统

php - 当 SimpleXML 解析带有特殊字符的 XML 时会发生什么?

php - 如何在Windows中使用命令行在服务器上编译iPhone应用程序源代码?

php - 命名空间在 Laravel 4 中不起作用?

session - 为什么 Passport-twitter 需要 session 支持

php - 如何通过用户名对 Sentinel 进行身份验证?