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

ajsmith_codes's avatar

Redirect if user is authenticated and accidentally goes back to login.

If a user is already logged in and goes to the login page by accident, how can I redirect them without logging in again?

Here is my RedirectIfAuthenticated:

 public function handle($request, Closure $next, $guard = null) {

        if (Auth::guard($guard)->check()) {

            $role = Auth::user()->roles()->first()->name;

            switch ($role) {
                case 'admin':
                    return redirect('/admin/dashboard');
                    break;

                case 'order-entry':
                    return redirect('/order-entry/dashboard');
                    break;

                case 'operations':
                    return redirect('/operations/dashboard');
                    break;

                case 'customer':
                    return redirect('/customer/dashboard');
                    break;

                default:
                    return redirect('/home');
                    break;
            }
        }
        return $next($request);
    }
0 likes
10 replies
larsverp's avatar

I don't seem to completely understand your question, but in my opinion there are some points of improvement in this code. To begin with, I noticed your 'standard' routes are actually the same as the name of their roles. And I personally hate to use a switch-case statement :) Instead you could do it like this:

 public function handle($request, Closure $next) {

        if (Auth::check()) {

            $role = Auth::user()->roles()->first()->name;

            return redirect('/'.$role.'/dashboard');
        }
		else {
			return redirect('/home')
		}
        return $next($request);
    }

Or maybe consider doing it like this to just point them back from where they came from:

public function handle($request, Closure $next) {

        if (Auth::check()) {
            return redirect()->back()
        }
		else {
			return redirect('/home')
		}
        return $next($request);
    }

ajsmith_codes's avatar

Hopefully this helps...

If I am logged in and accidentally go back to "/" I see the login form. In that case, I shouldn't see the form but should get redirected just like when I first logged in.

larsverp's avatar

Alright, in that case the code I provided should work. Just apply that middleware to the "/" route.

Snapey's avatar

you would just apply the redirectIfAuthenticated middleware to the / route

ajsmith_codes's avatar

I tried the suggestion but it doesn't work. It just stays on the login page with the form. Could it be because I am using $guard?

larsverp's avatar

Did you try just using if (Auth::check())

ajsmith_codes's avatar

Hello. Finally getting back to this. I still haven't solved it.

The only thing left that I think I didn't address was the route:

Route::get('/', function () {
  return view('auth.login');
});

Does that make a difference?

STEREOH's avatar

@ajsmith_codes you could simply do

Route::get('/', function () {
  if ( auth()->check()) return redirect('/home'); // the route you want the user to be redirected to.
  return view('auth.login');
});
Snapey's avatar
Route::get('/', function () {
  return view('auth.login');
})->middleware['guest']);
1 like
ajsmith_codes's avatar
ajsmith_codes
OP
Best Answer
Level 5

I finally figured this out. Due to the way I have my app set up, I had to redirect the user to a different link:

Route::get('/', function () {
    return redirect('/login');
});

Please or to participate in this conversation.