我在Laravel 7 Project中使用2种身份验证方法。默认身份验证和手动身份验证。用户使用默认身份验证,管理员使用默认身份验证。但是在手动身份验证中,当给出凭据时,路由会重定向回去。请任何人帮助我。我无法理解该错误,我在哪里遗漏了任何东西。
这是我的路线:
//Admin Authentication
Route::get('/admin', 'AdminController@index')->name('adminLogin');
Route::post('/adminLogin', 'AdminController@adminLogin');
Route::group(['middleware' => ['admin']], function () {
Route::get('/admin/home', 'AdminController@adminHome')->name('adminHome');
Route::post('adminLogout', 'AdminController@adminLogout');
});
管理员控制器:
public function adminLogin(Request $request)
{
$this->validator($request);
if(Auth::guard('admin')->attempt($request->only('email','password'),$request->filled('remember'))){
//Authentication passed...
return redirect('/admin/home')->with('success','You are Logged in as Admin!');
}
//Authentication failed...
return $this->loginFailed();
}
中间件:
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check()) {
return redirect('/admin');
}
return $next($request);
}
处理程序:
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
if ($request->is('admin') || $request->is('admin/*')) {
return redirect()->guest('/admin');
}
return redirect()->guest(route('index'));
}
登录表格:
<form role="form" action="{{ url('/adminLogin') }}" method="post">
@csrf
<div class="form-group">
<a href="#" class="pull-right label-forgot">Forgot email?</a>
<label for="inputUsernameEmail">Username or email</label>
<input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="form-group">
<a href="#" class="pull-right label-forgot">Forgot password?</a>
<label for="inputPassword">Password</label>
<input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="current-password">
@error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="checkbox pull-left">
<label>
<input type="checkbox">Remember me</label>
</div>
<button class="btn btn btn-primary pull-right" type="submit">
Log In
</button>
</form>