13

我对 Laravel 相当陌生,并且对记住我的功能有疑问。

通过将第二个参数附加到 Auth::attempt 方法,我已经成功地启用了“记住我”功能。

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
    // The user is being remembered...
}

如文档中所述,这可以无限期地记住我,或者直到用户手动注销。

我基本上想在“记住我”功能上设置一个过期日期。

查看控制台,我可以看到启用“记住我”会生成一个 remember_{HASH} cookie。

覆盖此cookie中指定的到期日期以假设未来一周的最佳方法是什么?cookie 当前将日期设置为过去,这使它永远存在。

请记住,我必须在 session.php 中设置 'lifetime' => 0 以便我可以根据用户偏好触发记住我的功能,因此在我的情况下将其更改为一周是行不通的。

谢谢!

4

1 回答 1

16

我不知道这是否是一个好方法,但如果你不顾一切地设置记住我的到期时间,你可以试试这个,不过它对我有用。

让 laravelAuth::atempt($credential,true)做它的正常业务,即将记住我的过期时间设置为5 年

我们将在App::after()方法中更改此设置,因此在您的filters.php文件中查找App::after()方法并更改 cookie 过期时间

App::after(function($request, $response)
{
  if ( Auth::check()){
      $ckname=Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
      $ckval=Cookie::get($ckname); //Get the value of the cookie
      return $response->withCookie(Cookie::make($ckname,$ckval,360)); //change the expiration time
  }
});

注意Cookie::make('name','value','expiration time');

于 2013-08-06T16:02:40.750 回答