Durgendra
Durgendra

Reputation: 145

Djoser: Password Reset with Username

I have developed an app using Django. It uses Djoser for user authentication.

Problem: I have allowed user to register with already existing email addresses i.e. there might be multiple users using same email ID. So I can't reset password with the email field. Either I must use username only or the combination of username and email.

How can I do this?

Note:- Using base endpoint /users/reset_password/ but it requires email only to reset the password.

Upvotes: 0

Views: 1208

Answers (1)

Aprimus
Aprimus

Reputation: 1551

One solution would be to add another field to djoser SendEmailResetSerializer. and perform additional validation in SendEmailResetSerializer.get_user method or validate method.

example:

  1. In settings file add the path to a custom SendEmailResetSerializer.
DJOSER = {
    'SERIALIZERS': {
        'password_reset': 'path.to.CustomSendEmailResetSerializer',
    },
}
  1. Define the custom serializer (inherit from djoser SendEmailResetSerializer.) and add a field, username for example.
class CustomSendEmailResetSerializer(SendEmailResetSerializer):
    username = serializers.CharField(required=True)
  1. Perform additional validation in get_user method or validate method.
class CustomSendEmailResetSerializer(SendEmailResetSerializer):
    username = serializers.CharField(required=True)

    def get_user(self, is_active=True):
        # Retrieve user here.
        return user

Upvotes: 1

Related Questions