2015-12-03 34 views
2

我需要幫助,所以當他登錄時出現了不同的錯誤,例如這是我的代碼,stil不知道如何返回不同的錯誤。如何在laravel上執行自定義auth :: attempt消息4

public function postLogin() 
    { 
     $user = array(
      'email'  => Input::get('email'), 
      'password' => Input::get('password'), 
      'active' => 1 
      ); 


     if (Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) { 
      return Redirect::route('home'); 
     } 

     // authentication failure! lets go back to the login page 
     return Redirect::route('login') 
      ->with('flash', ['message' => '<strong>Error!</strong> Account is not registered.', 
       'type'  => 'danger']); 
    } 

我的問題是如何添加這兩種自定義重定向

如果用戶有正確的用戶名/密碼,但active = 0,它將返回

return Redirect::route('login') 
      ->with('flash', ['message' => '<strong>Error!</strong> Account is not verified.', 
       'type'  => 'danger']); 

,如果用戶有錯誤的用戶名/密碼,它會像這樣返回

return Redirect::route('login') 
       ->with('flash', ['message' => '<strong>Error!</strong> Wrong password.', 
        'type'  => 'danger']); 

EDIT1

public function postLogin() 
    { 
     $user = array(
      'email'  => Input::get('email'), 
      'password' => Input::get('password'), 
      ); 


     if(Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) { 
      if(Auth::user()->active == 1) { 
       if(Auth::user()->isBanned == 0) { 
        return Redirect::route('home'); 
       } 

       Auth::logout(); 
       $message = '<strong>Error!</strong> Your account was banned for misconduct.'; 
      } 

      Auth::logout(); 
      $message = '<strong>Error!</strong> Account is not verified.'; 
     } else { 
      $message = '<strong>Error!</strong> Wrong password.'; 
     } 

     // authentication failure! lets go back to the login page 
     return Redirect::route('login') 
      ->with('flash', ['message' => $message, 
       'type'  => 'danger']); 
    } 

EDIT2

我的回答

public function postLogin() 
{ 
    $user = array(
     'email'  => Input::get('email'), 
     'password' => Input::get('password'), 
     ); 


    if(Auth::attempt($user, (Input::get('remember') == 'on') ? true : false)) { 
     if(Auth::user()->active == 1 && Auth::user()->isBanned == 0) { 
      return Redirect::route('home'); 
     } 

     if(Auth::user()->active == 0) { 
      $message = '<strong>Error!</strong> Account is not verified.'; 
     } else if (Auth::user()->isBanned == 1) { 
      $message = '<strong>Error!</strong> Your account was banned for misconduct.'; 
     } 

     Auth::logout(); 

    } else { 
     $message = '<strong>Error!</strong> Wrong password.'; 
    } 

    // authentication failure! lets go back to the login page 
    return Redirect::route('login') 
     ->with('flash', ['message' => $message, 
      'type'  => 'danger']); 
} 

回答

0

嘗試以下。

public function postLogin() 
{ 
    $user = array(
     'email'  => Input::get('email'), 
     'password' => Input::get('password') 
    ); 


    if (Auth::attempt($user, Input::has('remember'))) { 

     if (Auth::user()->active == 1 && Auth::user()->isBanned != 0) { 
      return Redirect::route('home'); 
     } 

     $message = '<strong>Error!</strong> Account is not verified.'; 

     if (Auth::user()->isBanned == 0) { 
      $message = '<strong>Error!</strong> Your account was banned for misconduct.'; 
     } 

     Auth::logout(); 

    } else { 
     $message = '<strong>Error!</strong> Wrong password.'; 
    } 

    return Redirect::route('login') 
     ->with('flash', [ 
      'message' => $message, 
      'type'  => 'danger' 
     ]); 

} 
+0

他仍然可以登錄不過, –

+0

因爲我有'路線::過濾器( '客人',函數(){ \t如果(AUTH ::檢查())返回重定向::路線(「家'); });'on'filter.php' –

+0

查看更新後的答案。在激活檢查後忘記註銷。 – chanafdo

相關問題