jpadilla / django-rest-framework-jwt

JSON Web Token Authentication support for Django REST Framework
http://jpadilla.github.io/django-rest-framework-jwt/
MIT License
3.19k stars 652 forks source link

Get user instance from JWT not from DB #298

Closed saber-solooki closed 7 years ago

saber-solooki commented 7 years ago

Hi. I read this issue and still I have some question. How can I do some updating user query without hitting DB for getting user instance? for example my JWT has user_id already inside token. I want to update email field of user and I can do this like this: User.objects.filter(id=user_id).update(email="example@test.com") ,but DRF by default hitting DB and get user instance when send request and we can get user instance like this: request.user. How I can change this behavior and avoiding hitting DB for getting user instance because we already have user_id and can do some stuff with it. thank's

angvp commented 7 years ago

Can you rephrase or re-edit your question not sure I can follow?

angvp commented 7 years ago

Sorry I couldn't understand well and after asking for clarification I haven't got yet an answer, please re-open if you still having this issue and provide better info i.e: "for example my JWT has user_id" ? jwt doesn't contain that, so please clarify for next time :D

fergyfresh commented 5 years ago

@saber-solooki I think I am following what your question was. You want to get the instance of the user from the ID and not from the Username, however you cannot get an instance of the user without hitting the DB unless you have an instance of that User living in memory. In which you could index into that data store by the user_id. Also, looking up a User by user_id should be O(1).