繁体   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