Djangorestframework-simplejwt: how to change the database table used by simplejwt to authenticate user for generating token instead of using auth_user table?

Created on 15 Jan 2021  路  19Comments  路  Source: jazzband/djangorestframework-simplejwt

I couldn't find the configuration to use some specific table to generate the token instead of auth_user table

question

All 19 comments

I don't think SimpleJWT offers any user model that isn't specified with settings.AUTH_USER_MODEL since we need to use the django.contrib.auth module. Did you set the AUTH_USER_MODEL and are you still using django.contrib.auth?

Then what if we have some another Model like Employee or Customer where we are managing email and password by our own??

@yash0307jain Did you find any solution?

@yash0307jain @deepanshu-nickelfox SimpleJWT is not an extensible framework; it was decided awhile ago it's a solution and not really something to extend. What you could do is go to https://github.com/SimpleJWT/django-rest-framework-simplejwt/blob/c684e8396f8446cc289ed73a0740ca222c85470d/rest_framework_simplejwt/authentication.py#L28 (authentication.py) and change the designated model. You would also need to reconfigure some other places like the default login rule and such.

I got the solution the owner already has added this feature...

https://github.com/SimpleJWT/django-rest-framework-simplejwt/issues/96#issuecomment-475717541

359

@Andrew-Chen-Wang @yash

Also, @Andrew-Chen-Wang , do i need to reset my token model
as i have changed the model but nothing happened...

can you please tell me Why??
@davesque

tl;dr Don't worry; that correct. You can make sure via assert type(request.user) == Model in your DRF view. TokenUser is not used for the default authentication class.

Probably not if you're referring to models.py. I believe TokenUser is only used for the other authentication type, but you're using the default which simply grabs the correct Django model.

Every time you authenticate and DRF calls get_user, if you've overriden the authentication.py via the permalink rather than that comment which is old now, then we're returning the specified model you've given.

Nope Nothing happend:

i am getting same error as Field 'id' expected a number but got '377f9e30-9eea-4cf1-a0c3-7bd969184f25'.
that uuid belongs to a user saved in a db.

Also, the request.user is AnonymousUser

also, i have created a new db, but nothing happend.

  1. Check your settings so that you're using the new authentication model instead of SimpleJWT's default
  2. Did you follow what I said or what the comment said? Because if you followed the comment, then you did it wrong since you're not properly overriding user_model.
  3. Are you using JWTAuthentication or the other one that subclasses JWTAuthentication? If it's the latter, then that's your problem

This is what i am doing:

`from rest_framework_simplejwt.authentication import JWTAuthentication
from apps.user import models

class CustomJWTAuthentication(JWTAuthentication):
user_class = models.User
`

also, i have override that settings

), 'DEFAULT_AUTHENTICATION_CLASSES': ( 'apps.common.authentication.CustomJWTAuthentication',

but i am not aware of your 3rd point sir

So like I said in Point 2, you didn't follow what I was saying. So you've overriden the class; now you need to override the __init__ magic method and set user_model after calling super. Ref the permalink from the above comment.

yeah man, i really tried

first by extending the :

from rest_framework_simplejwt.authentication import JWTAuthentication


class CustomJWTAuthentication(JWTAuthentication):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.user_class = user_model.User

i check user_class it is same as my model, but it still showing error......

after some time i found there is another user model in state.py
then another in some serializers it ask for username_field, then it looks for user.is_active, and chain goes on...........

any simple method to use our own model??

if you can share any snip done by you, it could be a really be a great help

You could also just set your model with AUTH_USER_MODEL in your settings so you don't have to go through all this trouble. Regarding is_active, in the settings you can change the login rule option to your own custom rule.

So again, re-read what I wrote here: https://github.com/SimpleJWT/django-rest-framework-simplejwt/issues/359#issuecomment-766874032

yeah i got it, i can't change my user model in middle of the project with some AUTH_USER_MODEL,

is there any other library, which i can use, see i am new to python and django so, i don't know about all these.

btw Thank you so much Mr. @Andrew-Chen-Wang for your help, you are such a great person
:fire:

I see. This library isn't meant to be extensible. Would I'd do is just override lots of the classes to suit my needs. There should be some Django OAuth library that could probably help. I think it's run by Jazzband, and typically their packages are extensible.

:+1:

Closing since this is the right answer:

So like I said in Point 2, you didn't follow what I was saying. So you've overriden the class; now you need to override the __init__ magic method and set user_model after calling super. Ref the permalink from the above comment.

Was this page helpful?
0 / 5 - 0 ratings