2014-10-31 125 views
3

創建postSignIn方法,並希望驗證: 電子郵件,密碼,verifiedFlag 首先是沒有問題創造postSignIn方法,如:Laravel驗證登錄解決方案

public function postSignIn(){ 
    if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){ 
      return Redirect::route('home-view'); 
    } 
    else{ 
     return "Email/Password wrong or Your Account not verified by Admin"; 
    } 
} 

但現在我儘量做到更用戶通過獨立的警報友好的

  • 帳戶未驗證,並
  • 電子郵件/密碼錯誤

,現在我儘量讓這樣的:

if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password')))){ 
     Auth::logout(); 
     if(Auth::attempt(array('nim' => Input::get('nim'),'password' => Input::get('password'),'verified' => 1))){ 
      return Redirect::route('home-view'); 
     } 
     else{ 
      return "Your Account not verfied. Please wait until admin verified your account or contact your admin"; 

     } 
    } 
    else{ 
     return "NIM/Password wrong"; 
    } 

沒有什麼問題,但我想我需要其他的解決方案,以便驗證不需要登錄(未遂)兩次

回答

3

你可以使用validate方法。這將工作:

public function postSignIn(){ 
    if(Auth::attempt(array('email' => Input::get('email'),'password' => Input::get('password'),'verifiedFlag'=>1))){ 
      return Redirect::route('home-view'); 
    } 
    elseif(Auth::validate(array('email' => Input::get('email'),'password' => Input::get('password')))){ 
      return "Your Account not verified by Admin"; 
    } 
    else 
    { 
     return "Email/Password wrong"; 
    } 
} 
+0

我只是意識到我錯誤的放置IF語句._。不管怎樣,謝謝 :) – GandhyOnly 2014-11-02 04:31:11

0

過濾器是要走的路。解決這個問題很簡單,很簡單,參見下面的示例。

如果用戶在任何時候都處於非活動狀態,它將註銷用戶, 您可以使用會話flash消息重定向用戶,您的登錄代碼將按原樣運行。

Route::filter('auth', function() 
{ 
    if (Auth::guest()) 
    { 
     if (Request::ajax()) 
     { 
      return Response::make('Unauthorized', 401); 
     } 
     else 
     { 
      return Redirect::guest('login'); 
     } 
} 
else 
{ 
    // If the user is not active any more, immidiately log out. 
    if(Auth::check() && !Auth::user()->verifiedFlag) 
    { 
     Auth::logout(); 
     Session::flash('message','Your account is not active, please contact your administrator    to active your account'); 

     // redirect to login page 
     return Redirect::to('/'); 
    } 
} 
});