In my Laravel 6 application, the login page located in laravel.test/login
. I also have a subdomain admin.laravel.test
. If a guest or not an administrator is trying to access admin.laravel.test
, they should be redirected to the login page.
So, I created a new middleware called CheckIfAdmin
.
public function handle($request, Closure $next)
{
if (!$request->user()) {
return redirect()->route('login');
}
if ($request->user() && $request->user()->role != UserRole::Administrator) {
return redirect()->route('member');
}
return $next($request);
}
In the routes\web.php
Route::group(['domain' => 'admin.laravel.test'], function() {
Route::group(['middleware' => 'admin'], function() {
Route::get('/', 'Admin\AdminController@index');
});
});
Login method
if ($this->guard()->user()->status == UserStatus::Active) {
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return redirect()->intended('/');
}
The problem is when the previous URL was admin.laravel.test
, it's not redirecting to admin.laravel.test
after authentication. Doing dd(redirect()->intended())
shows that the targetUrl was laravel.test
. Did I do something wrong here?
Funny, solved just by adding auth
middleware.
Route::group(['domain' => 'admin.laravel.test'], function() {
Route::group(['middleware' => ['admin', 'auth']], function() {
Route::get('/', 'Admin\AdminController@index');
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.