2014-08-28 46 views
-1

這是我的代碼Laravel驗證登錄返回特定的錯誤

public function login_Post() 
{ 
    if (Auth::user()->attempt(array('email' => Input::get('email'), 'password' => Input::get('password'), 'active' => 1))) 
    { 
     Event::fire('users.login'); 
     return Redirect::to('/')->with('message', 'succsed'); 
    } 
    else 
     return Redirect::to('user/login')->withInput(Input::except('password')); 
} 

我想提醒用戶,當他/她是不是積極主動,我怎麼能undrestand激活需要或登錄由錯誤的用戶名或密碼失敗?

回答

2

與你已有的代碼最簡單的方法是先Auth::attempt(),然後檢查active屬性,所以你可以返回一個單獨的錯誤。

控制器:

在錯誤
public function login_Post() 
{ 
    if (Auth::attempt(array('email' => Input::get('email'), 'password' => Input::get('password')))) 
    { 

     if(!Auth::user()->active) 
     { 
      Auth::logout(); 
      return Redirect::to('user/login')->with('errors', ['Your account hasn't been activated']); 

     } 

     Event::fire('users.login'); 
     return Redirect::to('/')->with('message', 'succsed'); 
    } 
    else 
     return Redirect::to('user/login')->withInput(Input::except('password'))->with('errors', ['Authentication failed.']);; 
} 

然後查看:

@if(Session::get('errors')) 

    <ul class="error"> 
     @foreach(Session::get('errors') as $message) 
      @if(is_array($message)) 
       @foreach($message as $subMessage) 
        <li>{{ $subMessage }}</li> 
       @endforeach 
      @else 
       <li>{{ $message }}</li> 
      @endif 
     @endforeach 
    </ul> 

@endif 
1

你必須檢查用戶是否存在第一:

if (! User::where('email', Input::get('email'))->first()) 
{ 
    return 'This user does not exists'; 
} 

然後嘗試:

Auth::user()->attempt(...);