How to change api_token column in token protection

Laravel 5.5

I want to change the direction of the api token used in TokenGaurd , therefore, I created a special defender called CafeTokenGaurd extends TokenGuard, I define the __construct function in it, like what I want, something like this:

public function __construct(UserProvider $provider, Request $request) { parent::__construct($provider, $request); $this->inputKey = 'api_key'; // I want changing this part $this->storageKey = 'api_key'; } 

Now I want to define api_key from the relation to the user table as follows:

 device_user table -> token 

I want to define specific tokens for each device user, and I want to set the key for entering and storing the api key in this column in the pivot table between users and devices,

how should i do that ?!

thanks

+2
source share
1 answer

Since you need to change the way you get the user from the database, you really need to create and use a custom UserProvider , not a custom Guard . You will need only special protection if you want to rename the input key or storage key from api_token .

So, you will need a new user class UserProvider , which knows how to get a user with the specified credentials (token), and you need to tell Auth to use the new user class UserProvider .

First, if you are still using Eloquent, start by creating a new UserProvider class that extends the base class EloquentUserProvider . In this example, it is created in app/Services/Auth/MyEloquentUserProvider.php . In this class, you need to override the retrieveByCredentials function with information on how to get the user with the token provided.

 namespace App\Services\Auth; use Illuminate\Auth\EloquentUserProvider; class MyEloquentUserProvider extends EloquentUserProvider { /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { if (empty($credentials)) { return; } // $credentials will be an array that looks like: // [ // 'api_token' => 'token-value', // ] // $this->createModel() will give you a new instance of the class // defined as the model in the auth config for your application. // Your logic to find the user with the given token goes here. // Return found user or null if not found. } } 

Once you have created your class, you need to tell Auth about it. You can do this in the boot() method of your AuthServiceProvider service AuthServiceProvider . In this example, the name "myeloquent" will be used, but you can use whatever you want (except for "eloquent" and "database").

 public function boot() { $this->registerPolicies(); Auth::provider('myeloquent', function($app, array $config) { return new \App\Services\Auth\MyEloquentUserProvider($app['hash'], $config['model']); }); } 

Finally, you need to tell Auth to use the new user myeloquent . This is done in the config/auth.php configuration file.

 'providers' => [ 'users' => [ 'driver' => 'myeloquent', // this is the provider name defined above 'model' => App\User::class, ], ], 

Read more about adding custom providers in here .

+3
source

Source: https://habr.com/ru/post/1240829/


All Articles