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 get user object via access_token? #140

Closed
xianfuxing opened this issue Jul 22, 2019 · 4 comments
Closed

How to get user object via access_token? #140

xianfuxing opened this issue Jul 22, 2019 · 4 comments

Comments

@xianfuxing
Copy link

Can I get the user object via access token?

@MarkYHZhang
Copy link

MarkYHZhang commented Jul 27, 2019

I have the same question.

@davesque
Copy link
Member

It depends on what you have access to in the current scope. If you're in the code of an authenticated view, your user has already been determined by django and the jwt plugin. You'd do something like this:

def my_view(request):
    request.user

If all you have is the base64 encoded access token string, you can decode it using the AccessToken class:

from restframework_simplejwt.tokens import AccessToken

token_str = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU'
access_token = AccessToken(token_str)
user = User.objects.get(access_token['user_id'])

@xianfuxing
Copy link
Author

user = User.objects.get(id=access_token['user_id']) , works fine.
@davesque Thank you.

@davesque
Copy link
Member

@xianfuxing How come the authentication framework isn't giving you a user object on the request?

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

No branches or pull requests

3 participants