2017-08-11 64 views
0

我使用的是Laravel 5.4,我想處理失敗的登錄錯誤消息。但我想讓它更自定義一點。我想顯示特殊錯誤,如(未找到用戶名)或(密碼錯誤)。如何處理laravel登錄錯誤,用戶名與密碼不同

我該怎麼辦?在我的刀片

protected function sendFailedLoginResponse(Request $request) 
{ 
    $errors = [$this->username() => trans('auth.failed')]; 

    if ($request->expectsJson()) { 
     return response()->json($errors, 422); 
    } 

    return redirect()->back() 
    ->withInput($request->only($this->username(), 'remember')) 
    ->withErrors($errors); 
} 

@if ($errors->first('phone')) 
    <ul> 
     @foreach($errors->all() as $error) 
      <li>{{ $errors->first('phone') }}</li> 
     @endforeach 
    </ul> 
@endif 

我的新的(我做到了,它的工作,檢查我的新失敗的登錄方法,這是正確的標準?)

失敗的登錄方法失敗的方法:

if (!User::where('phone', $request->phone)->first()){ 
    return redirect()->back() 
     ->withInput($request->only($this->username(), 'remember')) 
     ->withErrors([ 
      $this->username() => 'user not found', 
     ]); 
} 


if (!User::where('phone', $request->phone)->where('password', bcrypt($request->password))->first()){ 
    return redirect()->back() 
     ->withInput($request->only($this->username(), 'remember')) 
     ->withErrors([ 
      'password' => 'password is incorrect', 
     ]); 
} 

回答

0

你可以在你的資源更改默認的身份驗證驗證消息>郎>恩> passwords.php或auth.php文件

+0

但我唯一的錯誤消息是:auth.failed和它不在資源 –

+0

我建議你覆蓋自己的authcontroller postlogin函數並創建自己的自定義驗證來處理返回登錄到視圖的錯誤,而不是自己發送錯誤。 Laravel在這裏提供了一個用於自定義驗證的指南:https://laravel.com/docs/5.4/validation – claireckc

+0

有沒有簡單的方法來管理登錄錯誤? –

相關問題