I'm using Django Rest Framework and django-rest-framework-simplejwt for authentication. The data being sent is being consumed by react.
So far I've been able to subclass the TokenObtainPairSerializer
to return both the username and ID when logging in / getting a new token.
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
def validate(self, attrs):
data = super(MyTokenObtainPairSerializer, self).validate(attrs)
data.update({'user': self.user.username})
data.update({'id': self.user.id})
return data
I'm trying to have a users username and id returned when the token is refreshed as well. This, however, does not work:
class MyTokenRefreshSerializer(TokenRefreshSerializer):
def validate(self, attrs):
data = super(MyTokenRefreshSerializer, self).validate(attrs)
user = self.context['request'].user
data.update({'user': user.username})
data.update({'id': user.id})
return data
Here, the username is always returned as an empty string and the ID is always returned as null.
I've tried many different ways, including trying to update this information in the view itself, but can't seem to get it to work. I've noticed that TokenRefreshSerializer
subclasses serializers.Serializer
while TokenObtainPairSerializer subclasses TokenObtainSerializer
which itself authenticates a user using Django authentication. This appears to require a password (like when a user logs in) but obviously that wouldn't be provided when simply refreshing a token.
My question is, how can I return the username and id (or any arbitrary information) about the current user when the token is refreshed?
The follow-on question would be, is it more practical to simply save that information to localStorage on the front end when the user first logs in and have the state re-populate that data on each subsequent visit (until the user logs out, or the refresh token is no longer valid)?
Please Note: This is not a repost of similar questions such as this one, because I am targeting the REFRESHING of tokens. The initial TokenObtainPairView
was straightforward to subclass and implement.
to_representation()
method ofMyTokenRefreshSerializer
class – JPGself.user
onMyTokenRefreshSerializer
. Trying to access it will cause an error. Assigning it simply returns an empty string (if using therequest.user
to assign the value). – Hanzy