Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to change the database table used by simplejwt to authenticate user for generating token instead of using auth_user table? #359

Closed
yash0307jain opened this issue Jan 15, 2021 · 19 comments
Labels

Comments

@yash0307jain
Copy link

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

@Andrew-Chen-Wang
Copy link
Member

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?

@deepanshu-nickelfox
Copy link

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

@deepanshu-nickelfox
Copy link

@yash0307jain Did you find any solution?

@Andrew-Chen-Wang
Copy link
Member

@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.

@deepanshu-nickelfox
Copy link

deepanshu-nickelfox commented Jan 25, 2021

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

#96 (comment)
#359

@Andrew-Chen-Wang @yash

@deepanshu-nickelfox
Copy link

deepanshu-nickelfox commented Jan 25, 2021

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

@Andrew-Chen-Wang
Copy link
Member

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.

@deepanshu-nickelfox
Copy link

deepanshu-nickelfox commented Jan 25, 2021

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.

@Andrew-Chen-Wang
Copy link
Member

  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

@deepanshu-nickelfox
Copy link

deepanshu-nickelfox commented Jan 25, 2021

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

@Andrew-Chen-Wang
Copy link
Member

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.

@deepanshu-nickelfox
Copy link

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??

@deepanshu-nickelfox
Copy link

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

@Andrew-Chen-Wang
Copy link
Member

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: #359 (comment)

@deepanshu-nickelfox
Copy link

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.

@deepanshu-nickelfox
Copy link

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

@Andrew-Chen-Wang
Copy link
Member

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.

@deepanshu-nickelfox
Copy link

👍

@Andrew-Chen-Wang
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants