How can i use jwt authentication on two different models, e.g a Users model and an Admins model? I am using Dingo api with jwt.
Use App\Config;
Config::set('jwt.user', Client::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Client::class,
]]);
@ThiagoLovatine and what about in the routes that are protected with jwt?
You put the same code in the base controller (Create a custom base controller for those routes)
Like this
class Controller extends BaseController
{
function __construct()
{
Config::set('jwt.user', Client::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Client::class,
]]);
}
}
Or just create a middleware so you don't need the base controller
@ThiagoLovatine
i am using dingo api with jwt and this is how am trying to do it
//police related routes
$api->group(['middleware' => 'jwt.auth', 'prefix' => 'police'], function(Router $api) {
//set jwt to use Police model instead of user.
Config::set('auth.providers.users.model', \App\Police::class);
$api->post('location','App\Http\Controllers\Api\LocationController@PostPoliceLocation');
$api->get('protected', function() {
return response()->json([
'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
]);
});
$api->get('refresh', [
'middleware' => 'jwt.refresh',
function() {
return response()->json([
'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
]);
}
]);
});
//user related routes
$api->group(['middleware' => 'jwt.auth', 'prefix' => 'user'], function(Router $api) {
Config::set('auth.providers.users.model', \App\User::class);
$api->post('location', 'App\Http\Controllers\Api\LocationController@PostUserLocation');
$api->get('protected', function() {
return response()->json([
'message' => 'Access to protected resources granted! You are seeing this text as you provided the token correctly.'
]);
});
$api->get('refresh', [
'middleware' => 'jwt.refresh',
function() {
return response()->json([
'message' => 'By accessing this endpoint, you can refresh your access token at each request. Check out this response headers!'
]);
}
]);
});
but either one works or they both fail
In this way, the configuration that prevails is the last one entered in the router file
Hi Jafar, Is this issue solved, Can you please share the solution.
Hey, I have solved this my self, here is the solution, https://medium.com/@akgarg007/laravel-6-jwt-multi-model-authentication-d5b416c4cdb4
Most helpful comment
You put the same code in the base controller (Create a custom base controller for those routes)
Like this
class Controller extends BaseController
{
function __construct()
{
Config::set('jwt.user', Client::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Client::class,
]]);
}
}