【问题标题】:Laravel 4 Auth::attempt($userdata, false) authentication is still keptLaravel 4 Auth::attempt($userdata, false) 身份验证仍然保留
【发布时间】:2013-11-28 07:58:25
【问题描述】:

最近我决定在我的 Laravel 4 应用程序中添加“记住我”功能。 找到了合适的语法方法:

Auth::attempt(array $credentials = array(), $remember = false)

这是为满足我的需要而采用的:

Auth::attempt($userdata, Input::has('remember'))

应用程序保持 Auth 会话,即使在浏览器关闭后用户也得到了身份验证。

不过,我发现现在 Laravel 始终保持用户身份验证,无论“记住”复选标记是什么状态。

我已经尝试过:

Auth::attempt($userdata, false)

Auth::attempt($userdata,)

用户仍然通过浏览器会话进行身份验证!!! 现在,由于Auth::attempt($userdata) 没有保持身份验证会话,我觉得每当Auth::attempt 方法中有第二个参数的迹象时,Laravel 自动将其假定为“真”。 谁能澄清一下?

编辑: 为了让每个人都非常清楚,我将列出重新创建此行为的步骤:

  1. 退出应用程序Auth::logout();
  2. 再次登录Auth::attempt($userdata, false)
  3. 关闭再打开浏览器
  4. 转到应用网址。
  5. 应用程序已通过身份验证加载

这是我在这里的第一个问题,所以请耐心等待我:)

【问题讨论】:

    标签: authentication laravel laravel-4 eloquent remember-me


    【解决方案1】:

    编辑:OP 明确表示他正确调用了 Auth::logout(),因此编辑答案以包含“真实”答案。

    app/config/session/php 中的lifetime 值设置为0,以便在浏览器关闭时清除cookie。

    上一个答案

    这是Illuminate\Auth\Guard(其外观为Auth)类中的login方法,最终由Auth::attempt()调用。

    来源:http://laravel.com/api/source-class-Illuminate.Auth.Guard.html#263-291

     public function login(UserInterface $user, $remember = false)
     {
         $id = $user->getAuthIdentifier();
    
         $this->session->put($this->getName(), $id);
    
         // If the user should be permanently "remembered" by the application we will
         // queue a permanent cookie that contains the encrypted copy of the user
         // identifier. We will then decrypt this later to retrieve the users.
         if ($remember)
         {
             $this->queuedCookies[] = $this->createRecaller($id);
         }
    
         // If we have an event dispatcher instance set we will fire an event so that
         // any listeners will hook into the authentication events and run actions
         // based on the login and logout events fired from the guard instances.
         if (isset($this->events))
         {
             $this->events->fire('auth.login', array($user, $remember));
         }
    
         $this->setUser($user);
     }
    

    很明显,即使$remember设置为true时设置了cookie,当$remember设置为false或其他非真实值时,cookie本身也不会被清除。

    当您调用Auth::logout() 函数时,cookie 被清除。

    【讨论】:

    • 谢谢你的回答,我只是不明白你的意思——我说的是“干净”登录,事先没有设置任何 cookie。
    • 您上次登录后是否给logout()打过电话?
    • app/config/session.phplifetime 的值是多少?如果您想在浏览器关闭时清除 cookie,则必须将其设置为 0
    • 就在现场!我很久以前就设置为120,忘记了!现在一切都很好!谢谢!您可以编辑您的答案以包含解决方案,以防其他有相同问题的人来搜索? :)
    • lifetime 设置为0 与我的应用程序中的其他基于会话的功能相混淆,例如传递给视图的验证错误等。使用时要小心,我认为寿命为 60分钟(当用户不想被记住,或者没有勾选“记住我”复选框时)是正确的。我不知道它是在哪个版本中实现的,但有一个 expire_on_close 参数应该可以满足我们的要求,就在 lifetime 下方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2013-06-11
    • 2019-04-06
    • 2017-05-11
    • 2019-05-24
    相关资源
    最近更新 更多