Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shivam7414's avatar

Redirect to login route If unauthorized user try to hit 'auth:admin' middleware in laravel 11.

Hey, I am stuck in Laravel 11.

I define my custom route in the bootstrap/app.php file

app.php code

function (Router $router) { $router->middleware('web') ->group(base_path('routes/web.php'));

        $router->middleware('web')
            ->prefix('admin')
            ->group(base_path('routes/admin.php'));

        $router->middleware('web')
            ->prefix('vendor')
            ->group(base_path('routes/vendor.php'));
    },

Admin.php route file code Route::group(['prefix' => 'auth'], function () { Route::get('login', [AuthController::class, 'index']); Route::post('login', [AuthController::class, 'login'])->name('admin.login');

Route::post('logout', [AuthController::class, 'logout'])->name('admin.logout');

});

Route::group(['middleware' => 'auth:admin'], function () { Route::prefix('dashboard')->group(function () { Route::get('index', [DashboardController::class, 'index'])->name('admin.dashboard'); });

}

I want the unauthorized access dashboard route to automatically redirect on the login route. How do I do this type of thing in Laravel 11?

0 likes
9 replies
jlrdw's avatar

I suggest setting up authentication for logins and use authorization to determine what a logged in user can or cannot do.

https://laravel.com/docs/11.x/authorization

Do you have separate login pages? Sorry didn't totally understand the above.

shivam7414's avatar

Hello @jlrdw , In Laravel 10 can do the same thing, like this

$guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if ($guard == 'admin') {
			if (Auth::guard($guard)->check()) {
				return redirect(RouteServiceProvider::ADMIN_HOME);
			}
		}else{
			if (Auth::guard($guard)->check()) {
				return redirect(RouteServiceProvider::HOME);
			}
		}
    }

we use this code in RedirectIfAuthenticated.php middleware, how do I same thing in Laravel 11?

Snapey's avatar

@Jamalmousa he used the camera on the visitors laptop to decide if they LOOK LIKE an admin before directing them to one login form or the other.

Dont take advice from someone that thinks separate guards are a good idea.

1 like
shivam7414's avatar

@Jamalmousa Hii, I solve this problem like this, First I define all my routes in bootstrap/app.php

$router->middleware('web') ->group(base_path('routes/web.php'));

$router->middleware('web')
            ->prefix('admin')
            ->group(base_path('routes/admin.php'));

then I create custom guards

and create a middleware RedirectIfAuthenticated and register this middleware in bootstrap/app.php file like this 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

and this is my middleware code $guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if ($guard == 'admin') {
			if (Auth::guard($guard)->check()) {
				return redirect(RouteServiceProvider::ADMIN_HOME);
			}
		}else{
			if (Auth::guard($guard)->check()) {
				return redirect(RouteServiceProvider::HOME);
			}
		}
    }

    return $next($request);

and define route like this in web.php file Route::get('/', [AuthController::class, 'login'])->name('login')->middleware('guest');

shivam7414's avatar

@Snapey Hello sir,

Please advise us on the best way to manage the multi-auth system in Laravel, as I am new to it.

I know only the guard method for multi-auth.

Please or to participate in this conversation.