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

LaravelFan's avatar

Login link not showing on Laravel welcome screen

Hi,

I'm trying to create a dashboard for the user, the vendor, and the admin for a multi-vendor website I'm building. Inside the views folder I created an admin folder and a vendor folder. Each folder contains a dashbaord.blade.php file. Following the changes I made below, I can longer access the login page from the Laravel welcome screen when I visit: http://127.0.0.1:8001/

I've this code inside the RouteServiceProvider.php file:


<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to your application's "home" route.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
    public const HOME = '/dashboard';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     */
    public function boot(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));

            Route::middleware(['web', 'auth', 'role:admin'])
                ->prefix('admin')
                ->as('admin.')
                ->group(base_path('routes/admin.php'));

            Route::middleware(['web', 'auth', 'role:vendor'])
                ->prefix('vendor')
                ->as('vendor.')
                ->group(base_path('routes/vendor.php'));
        });
    }
}

The RoleMiddleware.php file contains the following:


<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class RoleMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next,$role): Response
    {
        if($request->user()->role !== $role){
            if($request->user()->role == 'vendor'){
                return redirect()->route('vendor.dashbaord');
            }elseif ($request->user()->role == 'admin'){
                return redirect()->route('admin.dashbaord');
            }else {
                return redirect()->route('user.dashboard');
            }
        }
        return $next($request);
    }
}

I added this to the Kernel.php file:


'role' => \App\Http\Middleware\RoleMiddleware::class,

The AuthenticatedSessionController.php file contains this:


<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;

class AuthenticatedSessionController extends Controller
{
    /**
     * Display the login view.
     */
    public function create(): View
    {
        return view('auth.login');
    }

    /**
     * Handle an incoming authentication request.
     */
    public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();
        if($request->user()->role === 'admin') {
            return redirect()->intended('/admin/dashboard');
        } elseif($request->user()->role === 'vendor') {
            return redirect()->intended('/vendor/dashboard');
        }
        return redirect()->intended(RouteServiceProvider::HOME);
    }

    /**
     * Destroy an authenticated session.
     */
    public function destroy(Request $request): RedirectResponse
    {
        Auth::guard('web')->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return redirect('/');
    }
}

The admin.php file contains this:


<?php

use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Route;

/** Admin Routes */
Route::get('dashboard', [AdminController::class, 'dashboard'])->name('dashboard'); 

The vendor.php file contains this:


<?php 

use App\Http\Controllers\VendorController;
use Illuminate\Support\Facades\Route;

/** Vendor Routes */
Route::get('dashboard', [VendorController::class, 'dashboard'])->name('dashboard'); 

The AdminController.php file contains this:


<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function dashboard()
    {
        return view('admin.dashboard');
    }

    public function login()
    {
        return view('admin.auth.login');
    }
}

The vendor.php file contains this:


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class VendorController extends Controller
{
    public function dashboard()
    {
        return view('vendor.dashboard');
    }
}

The web.php contains this (created by Breeze):


/** Laravel Breeze routes */
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__ . '/auth.php';


Grateful for your guidance.

0 likes
18 replies
LaravelFan's avatar

Update: I cleared the cache, logged out and the login link is now visible.

1 like
gych's avatar

@Mamunsson Ok does everything works as it should now? Its normal that when you're logged in that a login link won't be visible.

If you login to a website the login link is removed and you will only see the option to logout. After logging out the login link will become visible again and the logout link becomes invisible.

gych's avatar

@Mamunsson What does it show on the page? Does it show Dashboard instead? If it shows Dashboard it means that you are still logged in.

LaravelFan's avatar

@gych Many thanks for your guidance and support. Yes, It's taking me to the custom dashboard I built for both the admin and the vendor. I'll work on it and revert back to you.

Snapey's avatar

@Mamunsson you might want to search and replace in your project and fix instances of "dashbaord"

LaravelFan's avatar

I made some adjustments to the code:

  1. Currently, if the admin is logged in and tries to access the vendor dashboard he'll be directed back to the admin dashboard which is desirable. However, if the admin (while also logged in) tried to access the user dashboard, he'll be able to access it which not be the case.

  2. If the user is logged in and tried to access the admin dashboard he gets: Route [user.dashboard] not defined. The same message shows if the user is logged in and tries to access the the vendor dashboard.

  3. If the vendor is logged in and tries to access the user dashboard he gets page 404 not found. If the vendor tried to access the admin dashboard he is redirected to the vendor dashboard which is desirable.

The aim is to accomplish the following:

The admin should not be able to access the vendor or the user dashboard while logged in. The vendor should not be able to access the user or the admin dashboard while logged in. the user should not be able to access the admin or the vendor dashboard while logged in.

The admin.php file and the vendor.php file live inside the routes folder.

The RoleMiddleware.php has this code:


<?php

namespace App\Http\Middleware;

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

class RoleMiddleware
{
    public function handle(Request $request, Closure $next, $role)
    {
        $user = Auth::user();

        if ($user->role === 'admin' && $role !== 'admin') {
            return redirect()->route('admin.dashboard');
        } elseif ($user->role === 'vendor' && $role !== 'vendor') {
            return redirect()->route('vendor.dashboard');
        } elseif ($user->role === 'user' && $role !== 'user') {
            return redirect()->route('user.dashboard');
        }

        return $next($request);
    }
}

The RouteServiceProvider.php contains this:


<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to your application's "home" route.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
      public const HOME = '/user/dashboard';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     */
    public function boot(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));

            Route::middleware(['web', 'auth', 'role:admin'])
                ->prefix('admin')
                ->as('admin.')
                ->group(base_path('routes/admin.php'));

            Route::middleware(['web', 'auth', 'role:vendor'])
                ->prefix('vendor')
                ->as('vendor.')
                ->group(base_path('routes/vendor.php'));
        });
    }
}

The AuthenticatedSessionController.php contains this:

LaravelFan's avatar

The AdminController.php file contains this:


<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class AdminController extends Controller
{
    public function dashboard()
    {
        return view('admin.layouts.dashboard');
    }

}


gych's avatar

Update the role check in the roleMiddleware to this, I also simplified it

		if($role !== $user->role) {
        		if ($user->role === 'admin') return redirect()->route('admin.dashboard');
       			if ($user->role === 'vendor') return redirect()->route('vendor.dashboard');
        		if ($user->role === 'user') return redirect()->route('user.dashboard');
        }

For the error Route [user.dashboard] not defined can you share the route group or routes for user ?

gych's avatar

It could be simplified even more but that will be a bit less readable and maybe harder to work with / understand.

if($role !== $user->role) return redirect()->route($user->role . '.dashboard');

When using this make sure that the name for the route group of the specific user roles always matches the user role

LaravelFan's avatar

@gych Things look better now and there are only two issues:

1- If I'm logged in as admin and I go from admin/dashboard to user/dashboard, then I'll be directed to the user dashboard which is not desirable.

2- If I'm logged in as vendor and I go from vendor/dashboard to user/dashboard then I'll be directed to the user dashboard which is not desirable.

gych's avatar

@Mamunsson Ok does the middleware for user routes / user group contain the role:user like middleware(['web', 'auth', 'role:user']) ?

You didn't share the user routes or user route group so can't directly confirm what the exact issue is. Where is the route for user/dashboard added in your code, can you share it?

LaravelFan's avatar

@gych This is the complete RoleMiddleware.php:


<?php

namespace App\Http\Middleware;

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

class RoleMiddleware
{
    public function handle(Request $request, Closure $next, $role)
    {
        $user = Auth::user();

        if($role !== $user->role) {
            if ($user->role === 'admin') return redirect()->route('admin.dashboard');
               if ($user->role === 'vendor') return redirect()->route('vendor.dashboard');
            if ($user->role === 'user') return redirect()->route('user.dashboard');
    }


        return $next($request);
    }
}

Both the vendor.php and the admin.php are located inside the the routes folder.

This is the vendor.php file:


<?php 

use App\Http\Controllers\VendorController;
use Illuminate\Support\Facades\Route;

/** Vendor Routes */
Route::get('dashboard', [VendorController::class, 'dashboard'])->name('dashboard'); 

And this is the admin.php:


<?php

use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Route;

/** Admin Routes */
Route::get('dashboard', [AdminController::class, 'dashboard'])->name('dashboard'); 




gych's avatar

@Mamunsson And where are the routes for the user role? Like the route for user/dashboard its this route that is causing you the issue propably because the middleware role:user is not set for that route

LaravelFan's avatar

@gych The route for the user is located inside the web.php file:

/** Single Product route */
Route::get('/product/{id}', [ProductController::class, 'product']);


Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__ . '/auth.php';

Route::get('admin/login', [AdminController::class, 'login'])->name('admin.login');

/** Laravel Breeze routes */
Route::middleware(['auth', 'verified'])
    ->prefix('user')
    ->name('user.')
    ->group(function () {
        Route::get('dashboard', [UserDashboardController::class, 'index'])->name('dashboard');
        Route::get('profile', [UserProfileController::class, 'index'])->name('profile');
    });

gych's avatar
gych
Best Answer
Level 29

@mamunsson Add the middleware role:user to the route group for user

Route::middleware(['auth', 'verified', 'role:user'])
    ->prefix('user')
    ->name('user.')
    ->group(function () {
        Route::get('dashboard', [UserDashboardController::class, 'index'])->name('dashboard');
        Route::get('profile', [UserProfileController::class, 'index'])->name('profile');
    });
1 like
LaravelFan's avatar

@gych Awesome, many thanks indeed, that did it. I'm going to study this more carefully.

1 like
gych's avatar

@Mamunsson No problem :) Take your time, if you have more questions don't hesitate to reach out.

Please or to participate in this conversation.