2013-11-15 86 views
2

最近我决定添加一个“记住我”功能到我的Laravel 4应用程序。 与大成适当的方法发现:

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

这是像这样我需要通过:

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

应用不停的验证会话,并在浏览器关闭后,用户被认证。

虽然,我发现现在Laravel总是保持用户身份验证,无论状态是“记住”复选标记。

我试图做:

Auth::attempt($userdata, false) 

Auth::attempt($userdata,) 

用户跨浏览器会话还在验证! 现在,由于Auth::attempt($userdata)没有保留auth会话,我觉得只要有Auth::attempt方法中的第二个参数的迹象,Laravel auto会认为它是“真实的”。 任何人都可以澄清?

编辑: 要使它成为一个超级大家都很清楚,我将列出的步骤重新创建此行为:

  1. 应用Auth::logout();
  2. 登录再次Auth::attempt($userdata, false)
  3. 关闭和打开退出的浏览器
  4. 转到应用程序的URL。
  5. 加载应用程序认证

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

回答

4

编辑: OP明确表示他正确地呼叫Auth::logout(),因此编辑回答以包含“真实”答案。

lifetimeapp/config/session/php0值,使浏览器的cookie的密切清楚。

以前的答案

这是Illuminate\Auth\Guardlogin方法(这是facaded到Auth)类,它最终被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或其他非truthy值cookie自身不被清除。

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

+0

谢谢你的回答,我只是不确定你的意思 - 我正在谈论“干净”的登录,没有任何cookie设置之前。 –

+0

在上次登录后是否已经调用了'logout()'? – tharumax

+0

是的,我现在编辑了我的帖子,应该更清楚:) –