Search code examples
django-rest-frameworkdjango-rest-framework-simplejwt

Django RF simplejwt returns only token, not user object


I have React app and a Redux store. I am working on an authentication now. My backend is on Django RF and for JWT auth I use a simplejwt lib. The thing is, that this lib has an "out of the box" view (/token) that returns a JWT token on success.

The problem is that I need to have a user object in my app upon successful authentication. So when the user logs in it returns a token only. But I need to redirect the user to their page if logged in.

I sure can override this /token view and return whatever object I want, but why is this implemented this way right now?


Solution

  • Looks like there is no other way that to override validate() method in serializers.py like this:

    class UserTokenObtainPairSerializer(TokenObtainPairSerializer):
        def validate(self, attrs):
            data = {
                'token': super().validate(attrs),
                'id': self.user.id,
                'email': self.user.email,
                'name': self.user.name,
            }
            return data