【问题标题】:Laravel 5.7 Multi AuthenticationLaravel 5.7 多重身份验证
【发布时间】:2019-05-14 15:32:00
【问题描述】:

我在 Laravel 5.7 中创建了另一个身份验证作为管理员,除了它的默认身份验证。它们都正常运行,但是我创建的新身份验证没有显示错误消息

对于错误登录尝试,与默认身份验证一样。

这里是我为 Admin 模型创建的 LoginController 的代码。

public function showLoginForm(){
        return view('admin.login');
    }

    //Validate the form data
    public function login(Request $request){
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|string',

        ]);

        //Attempt to lo the Admin in
       $credential = [
            'email' => $request->email,
            'password' => $request->password,   
        ];
        //Attempt to log the Admin in
        if(Auth::guard('admin')->attempt($credential, $request->member)){
            return redirect()->intended(route('admin.home'));
        }
        //If unscuccessful, then redirect back to the login with the form data
        return redirect()->back()->withInput($request->only('email','remember'));
    }

请告诉我如何解决这个问题。谢谢。

【问题讨论】:

  • 你看这些表格的代码了吗?此外,分享您需要帮助的代码通常很重要,而不仅仅是屏幕截图。

标签: laravel laravel-5.7


【解决方案1】:

您需要验证这些数据

//取自默认的认证控制器

protected function validateLogin(Request $request)
    {
        $request->validate([
            $this->username() => 'required|string',
            'password' => 'required|string',
        ]);
    }

并在刀片文件中显示错误

比如

显示所有错误

@if ($errors->any())
        {{ implode('', $errors->all('<div>:message</div>')) }}
@endif

并显示具体的错误

<input type="text" name="firstname">
@if ($errors->has('firstname'))
    <div class="error">{{ $errors->first('firstname') }}</div>
@endif

如果您不清楚,请观看教程

https://pusher.com/tutorials/multiple-authentication-guards-laravel

【讨论】:

  • 下面的代码不正确吗? ` //验证表单数据 public function login(Request $request){ $this->validate($request, [ 'email' => 'required|email', 'password' => 'required|string', ]) ; $credential = [ 'email' => $request->email, 'password' => $request->password, ]; if(Auth::guard('admin')->attempt($credential, $request->member)){ return redirect()->intended(route('admin.home')); } return redirect()->back()->withInput($request->only('email','remember')); }`
猜你喜欢
  • 2023-03-14
  • 1970-01-01
  • 2019-06-14
  • 2019-04-10
  • 2018-07-21
  • 2018-10-17
  • 2019-04-30
  • 2019-05-18
  • 2015-08-21
相关资源
最近更新 更多