Tuesday, January 22, 2019

reset password , update password of logged in user

After user is logged in to the system they can update there password, this is the scenario now, lets start to dig in to the process:

after logged in , inside dash board :
somewhere you like ,
<a class="nav-link" href="{%url 'reset_account_password' %}"> Reset Password</a>

define the url in urls.py
path('ResetPassword/',views.reset_account_password,name="reset_account_password"),

Now, inside views.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode

def reset_account_password(request):
user=request.user
pwtoken=PasswordResetTokenGenerator().make_token(user)
newuid=urlsafe_base64_encode(force_bytes(user.pk)).decode()
return redirect('password_reset_confirm',uidb64=newuid,token=pwtoken)

For using this you need to import above lines in views.py

Its done , Simplest and secure way for doing ,
If you have any confussion then please leave comment below:

No comments:

Post a Comment