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

xeno's avatar
Level 1

Do I need to create a different login end points for different roles ?

Hello, this is the first time posting here in Laracasts. So please go easy on me.

I'm creating a application where the users can have a different roles but only a one-user-one-role. I was thinking how could I implement the Login feature with it. Do I need a to have a separate endpoints for a different role ? Or I can add a condition to check if the user is Role::Admin and redirect to its specific route.

Here is the snippet from my code.

public function store(LoginInterface $service) : RedirectResponse
{
	$service->authenticate();
		
	/**
	* @var \App\Models\User $user
	*/
	$user = auth()->user();

	if ($user->role !== Role::Admin) {
		return to_route('resident.dashboard');
	}

	return to_route('admin.dashboard');
} 

What do you think ? I was also thinking if I could create a sub-domain for admin role like admin.localhost:8000/login and use that for logging in as an admin instead of localhost:8000/login. I was really confused so I was asking which is better based on your experience.

0 likes
1 reply
LaryAI's avatar
Level 58

It is not necessary to create separate login endpoints for different roles. You can use the same login endpoint and add a condition to check the user's role and redirect them to their specific route.

In the provided code snippet, the condition checks if the user's role is not admin, and redirects them to the resident dashboard. If the user's role is admin, they are redirected to the admin dashboard. This approach is valid and can be used.

Regarding the sub-domain for the admin role, it is a good approach to separate the admin functionality from the rest of the application. You can create a sub-domain for the admin role and use it for logging in as an admin. This will make it easier to manage the admin functionality separately from the rest of the application.

Here's an example of how to create a sub-domain for the admin role in Laravel:

  1. Add a new route group for the admin sub-domain in your routes/web.php file:
Route::domain('admin.{domain}')->group(function () {
    // Admin routes here
});
  1. Create a new middleware for the admin sub-domain in app/Http/Middleware/AdminMiddleware.php:
namespace App\Http\Middleware;

use Closure;

class AdminMiddleware
{
    public function handle($request, Closure $next)
    {
        if (auth()->check() && auth()->user()->role === Role::Admin) {
            return $next($request);
        }

        return redirect()->route('login');
    }
}
  1. Add the new middleware to the admin route group in your routes/web.php file:
Route::domain('admin.{domain}')->middleware(['web', 'admin'])->group(function () {
    // Admin routes here
});
  1. Use the admin sub-domain for the admin login page:
<form action="http://admin.localhost:8000/login" method="post">
    <!-- Login form fields here -->
</form>

Note: Replace "localhost" with your actual domain name in the above examples.

Please or to participate in this conversation.