skip to Main Content

I am working on a Laravel project where I have three types of admins, each with their own login pages. Here are the routes:

// Core Admin Routes
Route::get('/core-admin', [CoreAuthController::class, 'loadLogin']);
Route::post('/core-admin', [CoreAuthController::class, 'login'])->name('login');
Route::middleware('auth')->group(function () {
    Route::get('/core-admin/dashboard', [CoreAuthController::class, 'coreAdmin']);
    Route::get('/core-admin/logout', [CoreAuthController::class, 'logout']); // Change logout route
});

// Master Admin Routes
Route::get('/master-admin', [MasterAuthController::class, 'loadLogin']);
Route::post('/master-admin', [MasterAuthController::class, 'login'])->name('login');
Route::middleware('auth')->group(function () {
    Route::get('/master-admin/dashboard', [MasterAuthController::class, 'masterAdmin']);
    Route::get('/master-admin/logout', [MasterAuthController::class, 'logout']); // Change logout route
});

// Website Admin Routes
Route::get('/website-admin', [WebsiteAuthController::class, 'loadLogin']);
Route::post('/website-admin', [WebsiteAuthController::class, 'login'])->name('login');
Route::middleware('auth')->group(function () {
    Route::get('/website-admin/dashboard', [WebsiteAuthController::class, 'websiteAdmin']);
    Route::get('/website-admin/logout', [WebsiteAuthController::class, 'logout']); // Change logout route
});

The issue I’m facing is that when I log in as a core-admin, I’m redirected to the website-admin/dashboard. I’ve checked my controller code and it seems fine, so I suspect the issue lies within the route files. Interestingly, if I comment out the route code for the website-admin, I’m then redirected to the master-admin/dashboard. Any insights on what could be causing this?

2

Answers


  1. i think you should provide more details especially regarding your auth middleware , im gonna provide you with an example on how to manage multi role using breeze in laravel

    first start by creating the role middleware in /app/Http/Midlleware

    class RoleMiddleware
    {
        /**
         * Handle an incoming request.
         *
         * @param  Closure(IlluminateHttpRequest): (SymfonyComponentHttpFoundationResponse)  $next
         */
        public function handle(Request $request, Closure $next ,$role): Response
        {
            if (auth()->user()->role_id != $role) {
                abort(403); // Forbidden access
            }
            return $next($request);
        }
    }
    

    Then register the middleware
    add this code to bootstrap/app

    
    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            web: __DIR__.'/../routes/web.php',
            commands: __DIR__.'/../routes/console.php',
            health: '/up',
        )
        ->withMiddleware(function (Middleware $middleware) {
            //
            $middleware->alias([
                'role' => AppHttpMiddlewareRoleMiddleware::class,
            ]);
        })
        ->withExceptions(function (Exceptions $exceptions) {
            //
        })->create();
    

    Then add this function to your user model
    to specify the redirect route of each user based on there role

       public function determineRedirectRoute()
        {
            return match ($this->role_id) {
                //routes names
                1 => 'agents_accounts.index',
                2 => 'accounts.index',
            };
        }
    

    finally modify your route file

    ///specify the role id for the corresponding route group
    Route::middleware(['auth' , 'role:2'])->group(function () {
        Route::get('accounts', [AccountsController::class, 'index'])->name('accounts.index');
    });
    
    // another role 
    Route::middleware(['auth' , 'role:1'])->group(function () {
        Route::get('agents/accounts', [AdminController::class, 'index'])->name('agents_accounts.index');
    
    });
    
    Login or Signup to reply.
  2. I think you need to change the order. I’ve had a similar problem before. You should try to change the order like this.

    // Master Admin Routes

    // Website Admin Routes

    // Core Admin Routes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search