Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
0359562311 opened this issue Mar 9, 2021 · 4 comments
Open

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

0359562311 opened this issue Mar 9, 2021 · 4 comments

Comments

@0359562311
Copy link

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

@iMerica
Copy link
Owner

iMerica commented Mar 10, 2021

Anything in #152 ?

@pierremonico
Copy link

#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

@aabanaag
Copy link

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?

@eliu-fracta
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants