2016-06-08 81 views
1

用戶在數據庫中有一個名爲verified的字段,它是一個布爾值。默認情況下它被設置爲0,並且當用戶在註冊時通過電子郵件發送給他們的驗證鏈接時,它被設置爲'1'。如何僅登錄已在Laravel 5.2中驗證的用戶

我需要將此布爾值設置爲0的用戶在嘗試登錄時被拒絕,並在屏幕上顯示一條消息以解釋爲什麼他們被拒絕。驗證設置爲'1'的用戶將照常登錄。

我已經寫在AuthController.php這種「認證」的方法,此方法是通過「handleUserWasAuthenticated」方法中利用在AuthenticatesUsers.php

public function authenticated($request, $user) 
    { 
     if($user->verified==false) 
     { 
      flash('you are not verified'); 
      return redirect('/login'); 
     } 

     return redirect('/'); 
    } 

這裏是利用它的「handleUserWasAuthenticated」方法。

protected function handleUserWasAuthenticated(Request $request, $throttles) 
{ 
    if ($throttles) { 
     $this->clearLoginAttempts($request); 
    } 

    if (method_exists($this, 'authenticated')) { 
     return $this->authenticated($request, Auth::guard($this->getGuard())->user()); 
    } 

    return redirect()->intended($this->redirectPath()); 
} 

這是目前不工作。已驗證的用戶正在登錄併發送到主頁。但是,未驗證的用戶也正在登錄,但返回到其他頁面。

我忘了提及,我沒有編寫上面的第二種方法,當我使用make:auth命令時,它與'AuthenticatesUser.php'中的其他方法一樣生成。

+0

使用驗證後嘗試鉤 - https://laravel.com/docs/5.2/validation。如果用戶已註冊但未通過驗證,首先檢查他是否已註冊(如果不立即將其丟棄),然後檢查是否已驗證,您可以顯示錯誤消息或停止登錄。 – Bugfixer

回答

0

authenticated方法火災給應用程序的用戶登錄後,所以你的問題是,你要發送的用戶登錄頁面登錄。如果一個未經驗證的用戶試圖訪問受保護的頁面,就會讓他看到它,因爲他是在登錄

一個可能的解決方案是註銷,如果用戶沒有通過驗證:

if($user->verified==false) { 
    \Auth::logout(); 
    flash('you are not verified'); 
    return redirect('/login'); 
} 
+0

謝謝。在AuthenticatesUsers特徵中是否有更好的方法來放置這些代碼? – DanielPahor

0

試試這個

if (Auth::user()->verified == 1) 
{ 
    return Redirect::to('/dashboard'); 
}