"Remember me" 不起作用,记住令牌总是空的

"Remember me" doesn't work, remember token always empty

“记住我”不起作用。即使在登录前检查,字段“remember_token”在数据库中始终为空,根本不记得用户。

这是我在 LoginController 文件中的登录方法

public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if ($this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

我尝试将记住我字段作为 attemptLogin 方法的第二个参数,但没有任何变化

Laravel 身份验证提供开箱即用的记住我功能。

要使用它,您需要做两件事:

1- 在您的用户中添加 remember_token 列 table - 这是存储令牌的地方

2- 将 true 作为 Auth::attempt() 的第二个参数传递以启用记住我的行为如果您这样做,Laravel 将生成一个令牌,该令牌将保存在用户 table 并在 cookie 中。在后续请求中,即使会话 cookie 不可用,只要记住我的 cookie 存在,用户就会自动进行身份验证。

您可以在文档中找到更多详细信息和示例:

https://laravel.com/docs/8.x/authentication#remembering-users

这段代码可能对您有所帮助,我从 laravel 示例项目中截取了它:

if ( Auth::attempt($this->only('email', 'password'), $this->filled('remember')) ){
    // logged in succcessfully, if remember key exist in request then remember me will be true
}