【问题标题】:Remembering Users not working with Laravel记住不使用 Laravel 的用户
【发布时间】:2019-11-09 06:16:18
【问题描述】:

我关注了manual,因为 我想在 Laravel 中使用 remember_me 令牌。为了验证它是否有效,我有一个测试middleware

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

稍后,在我的测试视图中,我有:

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

所以是的,我已通过身份验证,但不,我没有任何 remember_me cookie 集。我只看到会话 cookie。

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

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

'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());

其他一些类似的问题建议使用cookies 进行会话。我的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
]

【问题讨论】:

  • 将测试中间件放在 StartSession 之后或两个中间件之后怎么样?

标签: php laravel authentication cookies remember-me


【解决方案1】:

更改会话驱动程序以使用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

【讨论】:

  • @nowox 如果您仍然面临问题,请告诉我。
  • 希望你已经使用web.php 来定义路由。
  • 我想我有一个线索,我可能错过了AddQueuedCookiesToResponse
  • 你使用的是哪个 laravel 版本?
  • 最新版本:5.9
猜你喜欢
  • 2018-08-27
  • 2014-12-02
  • 2014-09-02
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多