djangorestframework-simplejwt
djangorestframework-simplejwt copied to clipboard
Obtain tokens of a user
How can I get all tokens of an specific user?
@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.
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)
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?