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

Drf how to: simple-jwt authenticating without the USERNAME_FIELD


I have extended the TokenObtainPairSerializer, my user model has the email as the USERNAME_FIELD but this type of user does not have an email instead I want to use an auto-generated unique id to authenticate in place of the email.

class MyTokenStudentSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        user = authenticate()(
            student_id=attrs['student_id'], password=attrs['password'])
        if user is not None:
            if user.is_active:
                data = super().validate(attrs)
                refresh = self.get_token(self.user)
                refresh['student_id'] = self.user.student_id
                try:
                    data["refresh"] = str(refresh)
                    data["access"] = str(refresh.access_token)
                    data['student_id'] = self.user.student_id
                    data['firstname'] = self.user.firstname
                    data['middlename'] = self.user.middlename
                    data['lastname'] = self.user.lastname
                    data['phone'] = self.user.phone
                    data['last_login'] = self.user.last_login
                    data['joined_date'] = self.user.joined_date
                except Exception as e:
                    raise serializers.ValidationError(
                        {'error': 'Something Wrong!'})
                return data
            else:
                raise serializers.ValidationError(
                    {'error': 'Account is not activated'})
        else:
            raise serializers.ValidationError({
                'error': 'Incorrect student id and password combination!'})

even tho i don't pass an email field this takes email and password, how do i get it to take the student_id instead of the email.


Solution

  • You can override the username_field as follows:

    Also be careful of using PasswordField, which trims whitespace by default. You definitely do not want password to be valid.

    from rest_framework import serializers
    rest_framework_simplejwt.serializers import PasswordField
    from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
    
    class MyTokenStudentSerializer(TokenObtainPairSerializer):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.fields['student_id'] = serializers.CharField(required=False)
            self.fields['password'] = PasswordField(trim_whitespace=False)
    
        username_field = 'student_id'
        auth_fields = ['student_id']