简体   繁体   中英

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 ie 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM