簡體   English   中英

如何處理laravel登錄錯誤,用戶名與密碼不同

[英]how to handle laravel login errors, username different from password

我正在使用Laravel 5.4,我想處理失敗的登錄錯誤消息。 但我想使其更加自定義。 我想顯示一些特殊錯誤,例如(找不到用戶名)或(密碼錯誤)。

我能做什么? (我做到了並且它正在運行,請檢查我新的失敗的登錄方法,這是否正確和標准?)

登錄方法失敗:

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',
        ]);
}

您可以在資源> lang> zh> passwords.php或auth.php文件中更改默認身份驗證消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM