Created
June 8, 2021 08:20
-
-
Save squio/28aadec528bea9302c168744fd0af3b6 to your computer and use it in GitHub Desktop.
Use url with uid and token generated by dj-rest-auth for actual password reset routine from django allauth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlpatterns = [ | |
# this path is used to generate email content for password reset request | |
# from dj-rest-auth API; format is same as used by default django auth so | |
# the generated URL must be translated to be used with allauth | |
path('accounts/password/reset/key/api/<uidb64>/<token>/', | |
user.ApiPasswordResetView.as_view(), | |
name='password_reset_confirm'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from allauth.account.forms import \ | |
default_token_generator as allauth_token_generator | |
from django.contrib.auth.tokens import default_token_generator | |
from django.utils.encoding import force_str | |
from django.utils.http import int_to_base36, urlsafe_base64_decode | |
from django.contrib.auth.models import User | |
class ApiPasswordResetView(View): | |
""" Wrapper to change the uid encoding and token | |
used by dj_rest_auth to the method used by allauth | |
""" | |
def get(self, *args, **kwargs): | |
uidb36 = None | |
token = kwargs.get('token') | |
try: | |
uid = force_str(urlsafe_base64_decode(kwargs.get('uidb64'))) | |
user = User.objects.get(pk=uid) | |
uidb36 = int_to_base36(int(uid)) | |
# validate default token | |
if default_token_generator.check_token(user, token): | |
# generate allauth token | |
token = allauth_token_generator.make_token(user) | |
else: | |
token = None | |
except (TypeError, ValueError, OverflowError, User.DoesNotExist): | |
return Http404() | |
# redirect to Allauth password reset link | |
return HttpResponseRedirect( | |
reverse_lazy('account_reset_password_from_key', args=[uidb36, token]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you so much for your response...
well what I was doing was having endpoints for a blog that a frontend will connect to so using dj-rest-auth
I had routes like
localhost:8000/api/v1/users/register
localhost:8000/api/v1/users/login # this was generated by dj-rest-auth for me
localhost:8000/api/v1/users/logout # this was generated by dj-rest-auth for me
localhost:8000/api/v1/users/register/verify-email/
localhost:8000/api/v1/users/register/verify-email/str:key/
localhost:8000/api/v1/users/password/reset/confirm/// # so this was the link that did not work till I used your snippets
and then had this in my route but on clicking the mail link it generates a template that was what I was referring to.
I am newbie so I guess my interpretation is not good forgive my naiveness
path('api/v1/users/password/reset/key/api///', views.ApiPasswordResetView.as_view(), name='password_reset_confirm'),