djangopython-3.xdjango-rest-frameworkdjango-rest-framework-jwt

How to return JWT token after user created in Django?


I am using Model Serializer and I want to return JWT Token after the user is created. Is there any way of doing it?
Thanks in advance

class UserSerializer(ModelSerializer):
    """Serializers for User objects"""

    class Meta:
        model = get_user_model()
        fields = ('email', 'password')
        extra_kwargs = {'password': {'write_only': True,
                                   'min_length': 5,
                                   'style': {
                                       'input_type': 'password'
                                   }}}

    def create(self, validated_data):
        """Create a new user with encrypter password"""
        user = get_user_model().objects.create_user(**validated_data)
        return user

Solution

  • I have achieved what I wanted using the code below. Thanks to @aman kumar. I am posting my own answer which refers for full working option that returns user email and the token.

    class UserSerializer(ModelSerializer):
        """Serilizers for User objects"""
        token = SerializerMethodField('get_auth_token')
    
        class Meta:
            model = get_user_model()
            fields = ('email', 'password', 'token')
            extra_kwargs = {'password': {'write_only': True,
                                         'min_length': 5,
                                         'style': {
                                             'input_type': 'password'
                                         }}}
    
        def create(self, validated_data):
            """Create a new user with encrypter password"""
            user = get_user_model().objects.create_user(**validated_data)
            return user
    
        def get_auth_token(self, obj):
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
            payload = jwt_payload_handler(obj)
            token = jwt_encode_handler(payload)
            return token
    
        def update(self, instance, validated_data):
            """Update a user setting the password correctly"""
            password = validated_data.pop('password', None)
            user = super().update(instance, validated_data)
    
            if password:
                user.set_password(password)
                user.save()
            return user