Jwt-auth: Change User Model

Created on 6 Jun 2017  路  14Comments  路  Source: tymondesigns/jwt-auth

Hi all,

I'm using:
"php": ">=5.5.9",
"laravel/framework": "5.2.",
"tymon/jwt-auth": "0.5.
"

Can I change user model from App/user to App/customer (for example)?
I've changed in config/jwt file, and not working.
I search in issues, but there's no valid answer. some people say it won't work, some say it work for them.
need some suggestion about this

Thanks,
Cheers,

Most helpful comment

Solved my issue:

Modified config/jwt.php with my custom model:
'user' => App\Models\Driver::class,

Added this line before the JWTAuth::attempt attempt in my controller:
config()->set( 'auth.defaults.guard', 'api' );

Added to config/auth.php > guards:

        'api' => [
            'driver'   => 'session',
            'provider' => 'drivers',
        ],

Added to config/auth.php > providers:

        'drivers' => [
            'driver' => 'eloquent',
            'model'  => App\Models\Driver::class,
        ],

All 14 comments

I have also encountered

"php": ">=5.6.9",
"laravel/framework": "5.4.",
"tymon/jwt-auth": "0.5."

@zanjs have you tried to update to 1.0 version?

@andikas No

@andikas you can try to edit the config/auth.php there is a config about customer user model

Also having an issue here. I have made the change to my custom model in config/jwt.php but the attempt is still looking in the users table not my custom one. This doesn't appear to be as simple as changing the config...

Hi @GregKeaney ,

Which laravel version do you use?
you can try with config before attempt the credentials with JWT.
Thats what I do to get it working

5.3. It works fine out of the box with the user model. I created a new model and tried using it by changing the 'user' => 'App\NewModel' in config/jwt.php but get an error (column not found in select) and could see it was still looking for the user model to auth. Seems you need to do more than just change the config file?

Another reason is to use the user object to create a license

$token = JWTAuth::fromUser($user)

try {
// attempt to verify the credentials and create a token for the user
if (!$token = JWTAuth::fromUser($user)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}

Has someone been able to solve this problem?

Same issue as @GregKeaney . Changed user setting in jwt.php but the query is still being made against the users table. Any idea why this is happening? Incidentally, the above comment from @EskimoLemon doesn't work for me. It just doesn't find the record - even if I hash the password first.

Solved my issue:

Modified config/jwt.php with my custom model:
'user' => App\Models\Driver::class,

Added this line before the JWTAuth::attempt attempt in my controller:
config()->set( 'auth.defaults.guard', 'api' );

Added to config/auth.php > guards:

        'api' => [
            'driver'   => 'session',
            'provider' => 'drivers',
        ],

Added to config/auth.php > providers:

        'drivers' => [
            'driver' => 'eloquent',
            'model'  => App\Models\Driver::class,
        ],

The @JamesPoel solution work fine for me on 0.5 version.

Especificaciones de mi proyecto:
"php": ">=5.6.4",
"laravel/framework": "
"tymon/jwt-auth": "0.5.*"

La solucion esta en el archivo: config/auth.php

En el array de providers => users => model (especificar el modelo de usuarios que desean consultar)

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\usuario::class,
],
],

Luego en ese modelo agregar:

use Illuminate\Foundation\Auth\User as Authenticatable;

y extender la clase por Authenticable:

class NOMBRE_CLASE extends Authenticatable

Y listo.

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gamelife1314 picture gamelife1314  路  3Comments

gandra picture gandra  路  3Comments

marciomansur picture marciomansur  路  3Comments

harveyslash picture harveyslash  路  3Comments

johncloud200 picture johncloud200  路  3Comments