Django Rest Framework + Django-Allauth Password Reset/Recovery

3.3k views Asked by At

I am trying to create a password recovery flow using Django Rest Framework and Django-Allauth.

Django-Allauth already does everything i need, my question is can i programatically call a django-allauth function or view from DRF that receives the email i want to reset and continues with the rest of the normal allauth flow (like creating the temporary tokens and sending the email to the client?

I don't see the point on having to rewrite all that code if one of the apps is doing everything i need. just need some help on how to "glue" them :)

2

There are 2 answers

3
psychok7 On BEST ANSWER

i figured it out

I added this to my DRF resource

@list_route(
    methods=['post'], permission_classes=[AllowAny],
    authentication_classes=[NoAuthentication]
)
def recover_password(self, request):
    if request.DATA.get('email'):
        # Lets be smart and reuse django-allauth password recovery system
        form = ResetPasswordForm({'email': request.DATA.get('email')})
        if form.is_valid():
            form.save()
            return Response(status=200)
    return Response(status=400)
3
adamtay82 On

In case it helps, solved this problem by just created a new class to override the password serializer.

from rest_auth.serializers import PasswordResetSerializer
from allauth.account.forms import ResetPasswordForm

class PasswordSerializer (PasswordResetSerializer):
    password_reset_form_class = ResetPasswordForm

Then make sure to add this to your settings:

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 'api.helpers.pwdreset.PasswordSerializer',
}