Jwt-auth: How to authenticate different models.

Created on 11 Jul 2017  路  8Comments  路  Source: tymondesigns/jwt-auth

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.

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,
]]);
}
}

All 8 comments

Use App\Config;

Config::set('jwt.user', Client::class);

Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Client::class,
]]);

  • You have to change Client:: to your model

@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

Was this page helpful?
0 / 5 - 0 ratings