我已经按照本教程中的描述设置了一个管理员身份验证防护: Laravel Multiple Auth Tutorial
Routes
Route::group([
'prefix' => 'admin',
'namespace' => 'Auth'
], function() {
Route::get('/login', 'AdminController@showLogin')->name('admin.show.login');
Route::post('/login', 'AdminController@login')->name('admin.login');
Route::group([
'middleware' => 'auth:admin'
], function() {
Route::get('/', 'AdminController@index')->name('admin.home');
Route::get('/logout', 'AdminController@logout');
});
});
控制器
class AdminController extends Controller
{
public function __construct()
{
$this->middleware('guest:admin')->except('logout');
}
public function index()
{
return "Admin authenticated";
}
public function showLogin()
{
return view('admin.login');
}
public function login(Request $request)
{
$admin = Admin::where('login_id', $request->input('login'))->firstOrFail();
Auth::guard('admin')->loginUsingId($admin->id, TRUE);
return redirect()->intended(route('admin.home'));
}
public function logout()
{
Auth::guard('admin')->logout();
return redirect()->route('admin.show.login');
}
}
RedirectIfAuthenticated 中间件
public function handle($request, Closure $next, $guard = null)
{
switch ($guard)
{
case 'admin':
if (Auth::guard($guard)->check())
{
return redirect()->route('admin.home');
}
break;
default:
if (Auth::guard($guard)->check()) {
return redirect('/home');
}
break;
}
return $next($request);
}
异常处理程序
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson())
{
return response()->json(['error' => 'Unauthenticated.'], 401);
}
当/admin没有在洛被访问时,所述/admin/login视图是正确。登录后,/adminURL 导致无限重定向循环错误。管理员可以使用/admin/logout路由成功注销。问题似乎是RedirectIfAuthenticated中间件的原因。我错过了什么吗?
回首忆惘然