dj-rest-auth icon indicating copy to clipboard operation
dj-rest-auth copied to clipboard

NoReverseMatch at /dj-rest-auth/password/reset/

Open 0359562311 opened this issue 3 years ago • 4 comments

I get this error: (Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name) while trying to make this request: image

0359562311 avatar Mar 09 '21 14:03 0359562311

Anything in https://github.com/iMerica/dj-rest-auth/issues/152 ?

iMerica avatar Mar 10 '21 21:03 iMerica

#152 definitely helped but after a few researches I just want to clarify for anyone that would struggle with that: dj-rest-auth makes use of Django internals, more specifically django.contrib.auth. This includes sending out an email that includes a url referring to a (Django) url with name= password_reset_confirm.

It was a bit difficult to find correct info even though this seems to be a common issue, so here are possible solutions:

Include your own dummy view

urlpatterns = [
...
    path('password-reset/confirm/<uidb64>/<token>/', TemplateView.as_view(), name='password_reset_confirm')
...
]

Use the urls from django.contrib.auth

urlpatterns = [
...
        url(r'^', include('django.contrib.auth.urls')),
...
]

Override the serializer (e.g. for a different frontend URL or custom logic) source

Override PASSWORD_RESET_SERIALIZER, with your own serializer.

REST_AUTH_SERIALIZERS = {
    'PASSWORD_RESET_SERIALIZER': 'path.to.your.CustomPasswordSerializer'
}

make this serializer inherit from the base and add your own custom save method that handles the custom logic.

from dj_rest_auth.registration.serializers import RegisterSerializer

class CustomRegistrationSerializer(RegisterSerializer):
  def save(self, request):
    #  get whatever data you need
    verify_url = ' <add your own custom url here>'
    # send the email using this new verify url

pierremonico avatar Mar 22 '21 20:03 pierremonico

I have the same exact issue, I added the password_reset_confirm URL on my urls.py also I have a prefix "api" on my route. Any help?

aabanaag avatar Jul 19 '21 14:07 aabanaag

I solved it with the approach below. not sure if there's a better solution.

# forms.py

class CustomAllAuthPasswordResetForm(AllAuthPasswordResetForm):

    def save(self, request, **kwargs):
        current_site = get_current_site(request)
        email = self.cleaned_data['email']
        token_generator = kwargs.get(
            'token_generator', default_token_generator)

        for user in self.users:

            temp_key = token_generator.make_token(user)

            # save it to the password reset model
            # password_reset = PasswordReset(user=user, temp_key=temp_key)
            # password_reset.save()

            # send the password reset email
            url = <Your password_reset_confirm URL>

            context = {
                'current_site': current_site,
                'user': user,
                'password_reset_url': url,
                'request': request,
            }
            if (
                allauth_account_settings.AUTHENTICATION_METHOD
                != allauth_account_settings.AuthenticationMethod.EMAIL
            ):
                context['username'] = user_username(user)
            get_adapter(request).send_mail(
                'account/email/password_reset_key', email, context
            )
        return self.cleaned_data['email']
# settings.py
REST_AUTH = {
    'USE_JWT': True,
    'TOKEN_MODEL': None,
    'JWT_AUTH_COOKIE': 'access',
    'JWT_AUTH_REFRESH_COOKIE': 'refresh',
    'JWT_AUTH_COOKIE_USE_CSRF': False,
    'JWT_AUTH_SAMESITE': 'None',
    'JWT_AUTH_SECURE': True,
    'REST_SESSION_LOGIN': False,
    'JWT_AUTH_COOKIE_ENFORCE_CSRF_ON_UNAUTHENTICATED': False,
    'PASSWORD_RESET_SERIALIZER': 'authentication.serializers.CustomPasswordResetSerializer',
}

# serializers.py
from authentication.forms import CustomAllAuthPasswordResetForm
class CustomPasswordResetSerializer(PasswordResetSerializer):

    @property
    def password_reset_form_class(self):
        return CustomAllAuthPasswordResetForm

eliu-fracta avatar Apr 18 '23 00:04 eliu-fracta