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

inzmam's avatar

Admin and user login redirection

In my project, i am having a user login page and admin login page. I want my admin login page to redirect to admin-login.blade.php view on successful authentication and redirect back to admin login page on unsucessful, and i also want it redirect to admin-dashboard.blade.php if admin is already authenticated not to the admin login page. I have written code in RedirectIfAuthenticated.php to make it happen as below.

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

            break;
        default:
            if (Auth::guard($guard)->check())
            {
            return redirect('/home');
            }
        break;
    }
    return $next($request);

And in Handler.php as below:-

if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); }

    $guard=array_get($exception->guards(),0);
    switch ($guard) {
        case 'admin':
            # code...
            $login='admin.login';
            break;
        
        default:
            # code...
            $login='login';
            break;
    }
    return redirect()->guest(route($login));

web.php

Route::get('/home', 'HomeController@index')->name('home'); Route::prefix('admin')->group(function() { Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login'); Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit'); Route::get('/', 'AdminController@index')->name('admin.dashboard'); });

0 likes
4 replies
inzmam's avatar

Somebody...please help me in this case..

Snapey's avatar

You have not stated what is wrong or what you want help with?

inzmam's avatar

When i log in as an admin it redirects me to admin dashboard but when i log it out it doesn't take me to admin logim page instead takes me to home page....

And when i login as a user, it takes me to admin dashboard which i don't want to happen...

bipin's avatar

@inzmam

   use this in your Auth/LoginController

       protected function authenticated(Request $request, $user)
          {
                 if(Auth::user()->user_type == '1')
                  {
                         return redirect()->route('admin');   
                  }
                 else(Auth::user()->user_type == '2')
                 {
                  return redirect()->route('normaluser');   
                }
         }

i have column user_type in user 1 is reserve for admin here 1 is inserted directly into database

Please or to participate in this conversation.