2013-11-15 85 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

是的,我現在編輯了我的帖子,應該更清楚:) –