guards laravel authenticated in a single table

175 Views Asked by At

I have a users table in which general data about users is stored and there is an admins table in which there is a key to the users table and what special data, how do I properly configure and implement guards so that for both guards the input occurs through the users table

or the best solution would be not to link these tables and completely separate the user and admins, that the admin is authorized through the admins table

1

There are 1 best solutions below

4
On BEST ANSWER

Since I have provided sufficient explanations in the comments, I will simply share the code blocks.

config/auth.php

guards' => [
    'jwt-admin' => [
        'driver' => 'jwt',
        'provider' => 'admin',
    ],
    'jwt-user' => [
        'driver' => 'jwt',
        'provider' => 'user',
    ],
],

'providers' => [
    'jwt-user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'jwt-admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
],

login controller

// $obj->jwt=auth("jwt-user")->login($personnel);
 $obj->jwt=auth("jwt-admin")->login($personnel);
 return response()->success($sessionObj);

middleware

// auth("jwt-user")->userOrFail();
auth("jwt-admin")->userOrFail();