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

sunilbfcj's avatar

Redirect after login according to user role laravel fortify

I want to redirect on different URLs after login. I am using laravel fortify. I am always redirecting on URL which is defined in config\fortify.php

'home' => RouteServiceProvider::HOME,

// or

'home' => '/dashboard'

but it must be a conditional redirect. I tried to redirect by using a middleware but not redirecting.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Sellers
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!Auth::check()) {
            return redirect()->route('login');
        }
        elseif(in_array(Auth::user()->role, ['grocery', 'restaurant', 'pharmacy', 'delivery', 'vegetables'])) {
            return redirect('dashboard');
        }
        return $next($request);
    }
}

Finally, I tried according to laravel official documentation but also failed. https://laravel.com/docs/8.x/authentication#redirecting-unauthenticated-users

Please help me, thanks in advance.

0 likes
9 replies
sunilbfcj's avatar
protected $routeMiddleware = [
        'sellers' => \App\Http\Middleware\Sellers::class,
];

in app\Http\Kernel.php

sunilbfcj's avatar

in my web.php

Route::group(['middleware' => ['sellers', 'verified'], 'verify' => true], function ()
{
    Route::get('/dashboard', function(){
        return view('dashboard');
    })->name('dashboard');

    Route::get('profile', function(){
        return view('users/profile');
    })->name('profile');

    Route::post('product/get/variants', [ProductController::class, 'getVariants']);

    Route::resources([
        'orders' => OrderController::class,
        'product' => ProductController::class,
    ]);

});
rodrigo.pedra's avatar

The sellers middleware applied to every route will make an infinite loop after the user logs in.

One easier thing you can try is create /redirect route and configure Fortify to redirect to it after login.

Then you can move your middleware logic to this new route.

So in your fortify configuration file you would have:

'home' => '/redirect'

Then you could create a route like this:

    Route::get('/redirect', function(){
        if (!Auth::check()) {
            return redirect()->route('login');
        }

        // you don't need else here as you are already returning
        // if the previous condition is true
        if(in_array(Auth::user()->role, ['grocery', 'restaurant', 'pharmacy', 'delivery', 'vegetables'])) {
            return redirect('dashboard');
        }

        return redirect('/'); // change to the regular user home page
    });
1 like
rodrigo.pedra's avatar

Not really,

You can use only the LoginResponse registration and add to any service provider's boot method.

For example in your AppServiceProvider you can add this to its boot method:

      // register new LoginResponse
        $this->app->singleton(
            \Laravel\Fortify\Contracts\LoginResponse::class,
            \App\Http\Responses\LoginResponse::class
        );

The other registration the blog post outlines is for additional features unrelated to redirecting after login

Chris1989's avatar

Hi i have the same problem , i want on admin to access on /customers and on user on /services but i get error when apply that on wep.php

Route::group(['middleware' => [
    'auth:sanctum',
    'verified',
    'accessrole',

]], function () {
  
    Route::get('/dashboard', function () {
        if (!Auth::check()) {
            return redirect()->route('login');
        }

     
        if (in_array(Auth::user()->role, ['user'])) {
            return  redirect('/services')->name('showservices');;
        }
      
        return redirect('/customers')->name('dashboard');

    });

With that setup , when the role is user , still try to find /dashboard doesn't redirect to /services also i tried to make extra setting on redirecticauthedicated.php but still dont work properly


 public function handle(Request $request, Closure $next, ...$guards)
    {
        $guards = empty($guards) ? [null] : $guards;

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

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

        return $next($request);
    }

Please or to participate in this conversation.