I'm new to Laravel 7 and i'm creating an application that requires 3 different user tables ( Admin , salesperson, warehouse manager ) Each user can login from a separate login form and has a different dashboard. I want to implement authentication for each user , to login to their specified dashboard , the users have different columns , which is why I wish to keep them in separate tables instead of one table with a role column.
Asked
Active
Viewed 6,010 times
1 Answers
2
you should add the three tables in the config/auth.php in the guards and providers to make the auth() function recognize each table: note to replace it with your models name
'guards' => [
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
'salesperson' => [
'driver' => 'session',
'provider' => 'salesperson',
],
'manager' => [
'driver' => 'session',
'provider' => 'manager',
],
],
'providers' => [
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'salesperson' => [
'driver' => 'eloquent',
'model' => App\Salesperson::class,
],
'manager' => [
'driver' => 'eloquent',
'model' => App\Manager::class,
],
],
After that you should create a middleware for each one to control the login, so you should add it in the middleware $routemiddleware function, In the kernel.php:
'admin' => \App\Http\Middleware\AuthenticateAdmin::class,
'salesperson' => \App\Http\Middleware\AuthenticateSalesperson::class,
'manager' => \App\Http\Middleware\AuthenticateManager::class,
create a controller for each one:
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Hash;
use App\Admin;
class LoginController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('guest');
}
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (auth()->guard('admin')->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
return redirect('*your route*');
} else {
dd('your username and password are wrong.');
}
}
public function getLogin()
{
return view('*the login form*');
}
}
and in your Admin.php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class Relative extends Authenticatable
{
use Notifiable;
protected $guarded = [];
protected $hidden = [
'password'
];
public function getAuthPassword()
{
return $this->password;
}
}
repeate the last two steps in for each one. If ypu need more information: https://laracasts.com/discuss/channels/laravel/multi-auth-login-with-single-table-user-using-middleware-in-laravel

Eman Emad
- 77
- 1
- 4
- 13