djangorestframework-simplejwt icon indicating copy to clipboard operation
djangorestframework-simplejwt copied to clipboard

Obtain tokens of a user

Open famdude opened this issue 2 years ago • 3 comments

How can I get all tokens of an specific user?

famdude avatar Jan 10 '23 13:01 famdude

@famdude What do you mean by get all tokens of a specific user ? JWT tokens are not stored anywhere, the simplejwt create the token on the fly, and you can use it to manage, validate tokens. JWTs contain the user information in their payload, which is signed with a secret key. This allows the server to verify their authenticity without the need to store them.

saedyousef avatar Mar 21 '23 12:03 saedyousef

If you want to have all tokens of all users, you can write a wrapper around the rest_framework_simplejwt.authentication.JWTAuthentication.authenticate and save user's tokens in a model, for example:

class UserJWT(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    token = models.JsonField()

class LoggedJWTAuthentication(authentication.JWTAuthentication):
    def authenticate(self, request):
        user_token = super().authenticate(request)
        if user_token:
            user, token = user_token
            UserJWT.objects.create(user=user, token=token)

likecodingloveproblems avatar Mar 21 '23 20:03 likecodingloveproblems

I think the question is to obtain the available access token by an existed User model?

https://django-rest-framework-simplejwt.readthedocs.io/en/latest/rest_framework_simplejwt.html

Per reading the document ? rest_framework_simplejwt.tokens.Token.for_user(user:AuthUser) would return the Token?

velly avatar May 12 '23 09:05 velly