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

alexteie's avatar

Laravel 11 and RedirectIfAuthenticated Middleware

Is there an way too accomplish this with laravel 11 i have this in laravel10 RedirectifAuthenticated middleware file? ' public function handle($request, Closure $next, $guard = null) { // if (Auth::guard($guard)->check()) { // return redirect(RouteServiceProvider::HOME); // }

    switch ($guard) {
        case 'lbenz':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('lbenz.dashboard');
            }

        case 'lbenz_contacts':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('lbenz.user.dashboard');
            }

        default:
            if (Auth::guard($guard)->check()) {
                //return redirect(RouteServiceProvider::HOME);
                return redirect()->route('zsc.dashboard');
            }

        break;
    }

    return $next($request);
}

'

0 likes
14 replies
LaryAI's avatar
Level 58

The code you've provided for the RedirectIfAuthenticated middleware in Laravel 11 is almost correct, but it's missing the break statements in the switch cases. Without the break statements, the code will fall through to the next case after executing the code for a matched case. Here's the corrected version of your middleware:

public function handle($request, Closure $next, $guard = null)
{
    switch ($guard) {
        case 'lbenz':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('lbenz.dashboard');
            }
            break; // Add this break

        case 'lbenz_contacts':
            if (Auth::guard($guard)->check()) {
                return redirect()->route('lbenz.user.dashboard');
            }
            break; // Add this break

        default:
            if (Auth::guard($guard)->check()) {
                return redirect()->route('zsc.dashboard');
            }
            // No need for a break here since it's the last case
    }

    return $next($request);
}

Make sure to import the Auth facade at the top of your middleware file if it's not already there:

use Illuminate\Support\Facades\Auth;

This middleware will now correctly redirect authenticated users based on the guard that is being used. If the guard is 'lbenz', it will redirect to the 'lbenz.dashboard' route. If the guard is 'lbenz_contacts', it will redirect to the 'lbenz.user.dashboard' route. For any other guard, it will redirect to the 'zsc.dashboard' route. If no guard is authenticated, it will simply pass the request to the next middleware in the stack.

alexteie's avatar

this doesnt help the redirectifauthenticated.php file wont be read? ids redirects me to /login all the time?

plegenza's avatar

Hello,

You can provide redirectCallback by:

\Illuminate\Auth\Middleware\RedirectIfAuthenticated::redirectUsing()

In AppServiceProvider (or other).

alexteie's avatar

@plegenza this doesnt work: ` public function boot(): void { \Illuminate\Auth\Middleware\RedirectIfAuthenticated::redirectUsing(function ($request, $guard = null) { switch ($guard) { case 'lbenz': if (Auth::guard($guard)->check()) { return redirect()->route('lbenz.dashboard'); } break;

            case 'lbenz_contacts':
                if (Auth::guard($guard)->check()) {
                    return redirect()->route('lbenz.user.dashboard');
                }
            break;

            default:
                if (Auth::guard($guard)->check()) {
                    //return redirect(RouteServiceProvider::HOME);
                    return redirect()->route('zsc.dashboard');
                }
            break;
        }
    });
}

'

alexteie's avatar

But in the appserviceprovider there is no $guard ? so this switch wont work?

plegenza's avatar

You can grab guard by using facade Auth::guard().

Example code for your problem:

        RedirectIfAuthenticated::redirectUsing(function() {
            $currentGuard = Auth::guard();

            switch ($currentGuard->name) {
                case 'lbenz':
                    return redirect()->route('lbenz.dashboard');

                case 'lbenz_contacts':
                    return redirect()->route('lbenz.user.dashboard');

                default:
                    return redirect()->route('zsc.dashboard');
            }
        });
jlrdw's avatar

Redirect using authorization with role, you don't need multiple guards.

1 like
Snapey's avatar

Do you know what redirectIfAuthenticated is for?

No, its not for redirecting WHEN you are authenticated.

Its only job is to stop the user reaching pages that are no use to a user that is logged in, such as login and register pages.

alexteie's avatar

@Snapey i know so anyone who is not authenticated for certain guard needs te redirect to there login page instead of standard /login for each guest

alexteie's avatar

but that redirect to de same login form, i want every auth:guard not authenticated to redirect to their own login route indstead of de default login route

alexteie's avatar

hi all just an question, how do i do this with de new laravel 12 method

$middleware->redirectTo( function($request) {

	$guard = Arr::get($exception->guards(), 0);

	switch ($guard) {
            case 'lbenz':
                $login = 'lbenz_login';
                break;

            case 'lbenz-api':
                $login = 'lbenz.api.login';
                break;

            case 'contacts':
                $login = 'lbenz.user.login';
                break;

            case 'contacts-api':
                $login = 'lbenz.api.user.login';
                break;

            case 'contacts_ldap':
                $login = 'lbenz.user.login';
                break;

            default:
                $login = 'zsc_login';
                break;
        }

        return redirect()->guest(route($login));

});

doesnt work?

martinbean's avatar

@alexteie Please use roles, not “guards”, to determine what a user can and cannot do. You’d then be able to redirect to the appropriate location based on the role(s) the user has.

alexteie's avatar

@martinbean yes if i had one table of users yes ;) but i need 2 separated guaard for ERP users and client users the erpusers dont need to te in the clients table..

Please or to participate in this conversation.