I have a Django project with a custom user model called CustomUser which extends AbstractBaseUser, which uses the phone number as the unique identifier. However, I'm encountering an authentication error when trying to authenticate a user using the phone number and password.

The error message states "User matching query does not exist," even though the user with the provided phone number and password exists in the phone_number_custom_user table.

The issue seems to arise when the get_by_natural_key method is called in the ModelBackend, specifically at the line user = UserModel._default_manager.get_by_natural_key(username). Despite the user existing in the phone_number_custom_user table, the User.DoesNotExist exception is raised.

I get the error in this django contrib auth backend.py file -


class ModelBackend(BaseBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        if username is None:
            username = kwargs.get(UserModel.USERNAME_FIELD)
        if username is None or password is None:
            return
        try:
            user = UserModel._default_manager.get_by_natural_key(username)
        except UserModel.DoesNotExist:
            # Run the default password hasher once to reduce the timing
            # difference between an existing and a nonexistent user (#20760).
            UserModel().set_password(password)
        else:
            if user.check_password(password) and self.user_can_authenticate(user):
                return user

Model Code -

class CustomUser(AbstractBaseUser):
    phone_number = models.CharField(max_length=20,unique=True, primary_key=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    #refresh_token = models.CharField(max_length=255, default=None)
    #access_token = models.CharField(max_length=255, default=None)

    USERNAME_FIELD = 'phone_number'

    objects = CustomUserManager()

Serializer -

class CustomUserLoginSerializer(serializers.Serializer):
    phone_number = serializers.CharField(max_length=255)
    password = serializers.CharField(max_length=255)

    def validate(self, data):
        phone_number = data.get('phone_number')
        password = data.get('password')
        request = self.context.get('request')

        if phone_number and password:
            user = authenticate(request,username=phone_number, password=password) 

            if user:
                return user
            else:
                raise serializers.ValidationError('Invalid phone number or password.')
        else:
            raise serializers.ValidationError('Phone number and password are required.')
0

There are 0 answers