简体   繁体   English

如何处理laravel登录错误,用户名与密码不同

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

I'm using Laravel 5.4 and I want to handle failed login errors message. 我正在使用Laravel 5.4,我想处理失败的登录错误消息。 But I want make it a little more custom. 但我想使其更加自定义。 I want to show special errors like (username not found) or (password is wrong). 我想显示一些特殊错误,例如(找不到用户名)或(密码错误)。

What can I do? 我能做什么? (I did it and it's working, check my new failed login method, is this correct and standard?) (我做到了并且它正在运行,请检查我新的失败的登录方法,这是否正确和标准?)

Failed login method: 登录方法失败:

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);
}

in my blade: 在我的刀片中:

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

my new failed method: 我的新失败方法:

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