0

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

  • In fact, both admins and regular users are users, and they can both be handled using the same controller and middleware for login processes without any issues. However, later on, if you want to apply specific middleware, such as 'AdminMiddleware,' for admin-related routes and operations, you can achieve this by adding middleware checks to the routes/web.php (or api.php) file. – icsarisakal May 26 '23 at 11:36
  • Additionally, if my previous explanation about multi-auth is insufficient, you can create your own custom auth object in addition to the default guard in the 'config/auth.php' file. Later on, you can use commands like auth('admin')->attempt() to customize it according to your needs. – icsarisakal May 26 '23 at 11:38
  • 1
    to separate access, I want to use guards, but I can't configure them so that they are like different models, but for users and admins authorization occurred through one table – Sergei Semenets May 26 '23 at 11:39
  • 1
    Actually, you can do it. I recently set up a structure in our company that fits your requirements. I'll share an example with you. -- 'defaults' => [ 'guard' => 'web', 'passwords' => 'main.users', ], -- 'guards' => [ 'jwt-admin' => [ 'driver' => 'jwt', 'provider' => 'admin', ], 'jwt-user' => [ 'driver' => 'jwt', 'provider' => 'user', ], ], -- – icsarisakal May 26 '23 at 11:44
  • 1
    'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'admin' => [ 'driver' => 'eloquent', 'model' => \App\Models\Admin::class, ], ], – icsarisakal May 26 '23 at 11:45
  • 1
    If you'd like, I can provide a more detailed response in writing. – icsarisakal May 26 '23 at 11:46
  • 1
    it would be great) as I understand it, your users log in using their own table, and admins on their own and tables are not linked by keys? – Sergei Semenets May 26 '23 at 11:48
  • 1
    Absolutely, but you can also achieve this by adding a key or flag to the same table. You just need to make customizations in your model. In our system, we separated them for the sake of enhanced security and for the convenience of future developers' quick adaptation. We added highly specialized policies for both. However, you are not obliged to do it this way. – icsarisakal May 26 '23 at 11:53
  • 1
    actually, I think this is the simplest, optimal, safer and more scalable way. I think that I will do so and completely split the data into different tables, even if the columns are repeated, I think it's not critical – Sergei Semenets May 26 '23 at 11:56

1 Answers1

2

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();
icsarisakal
  • 193
  • 7