我已经在Laravel中为具有两个表的管理员和用户进行了多AUTH认证,但是在登录时,如果我以登录到仪表板的形式输入任何密码,并且尝试从仪表板访问任何链接,都将出现验证错误,将我重定向回登录?
LoginController LoginController
Redirect If Authenticated RedirectIfAuthenticated
Authenticate Authenticate
AUTH Auth file
Route Admin Route
答案 0 :(得分:0)
仔细查看您的LoginToDashboard函数。您已经声明了名为admins的后卫,但是当您签入此功能时,您正尝试使用称为Admin的功能。因此,它与您声明的守卫不匹配。所以, 您可以像下面这样使LoginToDashboard函数->
//perform login here
public function LoginToDashboard(Request $request){
//validation part
$this->validate($request,[
'email'=>'required|email',
'password'=>'required|min:8'
]);
//need to match your user's credentials
if(Auth::guard('admin')->attempt(['email'=>$request->email,'password'=>$request->password],$request->remember)){
return redirect()->intended(route('admin.dashboard'));
}
return redirect()->back()->withInupt($request->only('email','remember'));
}
现在使您的auth.php页面像这样->
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
],
现在来到这里名为RedirectIfAuthenticated的页面,并像这样->
更改处理函数 public function handle($request, Closure $next, $guard = null)
{
switch ($guard){
case 'admin':
if(Auth::guard($guard)->check()){
return redirect()->route('admin.dashboard');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
break;
}
return $next($request);
}
}
最后进入这一部分,即如果用户未通过身份验证,那将是什么方法。您可以在handler.php页面中添加这样的功能
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 401);
}
$guard=Arr::get($exception->guards(),0);
switch($guard){
case'admin':
$login='admin/login';
break;
default:
$login='login';
break;
}
return redirect()->guest($login);
}
我认为您已经得到了答案。让我知道您的进一步改进。