password reset implementation with djoser

∥☆過路亽.° 提交于 2019-12-25 01:04:02

问题


I wanted to use djoser for the reset password functionality and as per the documentation:

PASSWORD_RESET_CONFIRM_URL

URL to your frontend password reset page. It should contain {uid} and {token} placeholders, e.g. #/password-reset/{uid}/{token}. You should pass uid and token to reset password confirmation endpoint.

I have done the following:

PASSWORD_RESET_CONFIRM_URL': 'reset/password/reset/confirm/{uid}/{token}',

url

url(r'^reset/password/reset/confirm/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', PasswordResetView.as_view(),),

View :

class PasswordResetView(APIView):

   def get (self, request, uid, token):
       post_data = {'uid': uid, 'token': token}
       return Response(post_data)

In my mail I get this link : http://127.0.0.1:8000/reset/password/reset/confirm/Mjk/538-954dccbc1b06171eff4d

This is obvious that I will get :

{
"uid": "Mjk",
"token": "538-954dccbc1b06171eff4d"

}

as my output but I wanted to go to auth/password/reset/confirm when the user clicks the link in the mail.


回答1:


Lets describe the actions first:

  1. The user clicks on the link to reset the password. reset password
  2. (Here you need a form to obtain a username or email address, depending on your settings) The user enters the username and clicks Submit.
  3. The user receives an email with a link to reset the password.
  4. The link opens the browser, which contains the form "Create a new password".
  5. The user enters a new password and sends a form
  6. The browser redirects the page to the home page and gives feedback that the password has been reset.

You can then use following method to reset the password.

#template
<p>Use the form below to change your password. Your password cannot be the same as your username.</p>
<form role="form" method="post">
  {% csrf_token %}
  <input type="password" name="password1" placeholder="New Password">
  <input type="submit">
</form>

#view
from django.shortcuts import redirect, render
from djoser.conf import django_settings

def reset_user_password(request, uid, token):
    if request.POST:
        password = request.POST.get('password1')
        payload = {'uid': uid, 'token': token, 'new_password': password}

        url = '{0}://{1}{2}'.format(
            django_settings.PROTOCOL, django_settings.DOMAIN, reverse('password_reset_confirm'))

        response = requests.post(url, data=payload)
        if response.status_code == 204:
            # Give some feedback to the user. For instance:
            # https://docs.djangoproject.com/en/2.2/ref/contrib/messages/
            messages.success(request, 'Your password has been reset successfully!')
            return redirect('home')
        else:
            return Response(response.json())
    else:
        return render(request, 'templates/reset_password.html')


来源:https://stackoverflow.com/questions/54322872/password-reset-implementation-with-djoser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!