0
votes

I've been trying forever to figure out how to create separate login views for different types of users in Laravel. I have one User model and then a roles table and role_users table. I do need to have users pick how they would like to login (i.e. as an Advisor, HR Admin, Employee). For example, one user can have both the Employee and HR Admin Role.

Here is what I attempted, using Laravel 7:

  • Install the Auth scaffolding
  • Create view called advisor-login by copying the login view that comes with basic Laravel Auth
  • Create AdvisorLoginController by copying the LoginController that comes with basic Laravel Auth
  • Create advisor-login route that directs to the AdvisorLoginController. Update the action in the advisor-login view to go to this route

In the AdvisorLoginController, we have:

use AuthenticatesUsers;

/**
 * Where to redirect users after login.
 *
 * @var string
 */
protected $redirectTo = RouteServiceProvider::HOME;

The "protected $redirectTo = RouteServiceProvider::HOME;" part is relatively straightforward as I could modify this to go to the Advisor Dashboard after logged in.

But the AuthenticatedUsers trait presents a problem. I went down this rabbit hole and couldn't get out. I tried to copy the AuthenticateUsers files from with the Laravel/UI package and create an AuthenticateAdvisors trait, but this became a mess.

Am I missing an easy fix here?

2
"But the AuthenticatedUsers trait presents a problem." What problem does it present? You could just create your own trait that overrides the functions causing you problems.miken32

2 Answers

1
votes

It can be done by providing multiple guards, for instance, one guard for admin and another for the user or customer, Then you should create a separate login controller in a different namespaces(it is recommened to be in different namespaces).

First, create a different guard for each in config/auth.php like below(you might want to use differnt driver in guard)

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'admins' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

you should group your routes by providing namespaces for different user types

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
//your route goes here!
});

by grouping routes you can have multiple logins form. for authenticating the different user types you can create a different middleware for each type like admin for instance and

<?php

namespace App\Http\Middleware;
use Closure;

class Admin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!auth()->user()->isAdmin()) {
            return response()->json(['message' => 'You have no rights'], 403);
        }
        return $next($request);
    }
}

then add the admin middleware to your routes for validatiating the roles and other stuff. like below

Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
    //your route goes here!
       Route::group(['prefix' => 'dashboard', 'middleware' => ['auth','admin']], function () {
          //your route goes here!
       });
  });
-1
votes

In my opinion wanting to create a specific login page for each user profile is not very optimal. there can be only one page but the access to functionality is managed by ACL. There may be an administration area where you can define and assign roles