Our team works on project with django-rest-api on backend and react on frontend. For authentication we use django-rest-auth, and we have problem with password reset. Here urls:
urlpatterns += [
url(r'^accounts/', include('allauth.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^api/', include('api.urls')),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(
r'^rest-auth/password/reset/$',
PasswordResetView.as_view(),
name='password_reset',
),
url(r'^rest-auth/password/reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
PasswordResetConfirmView.as_view(),
name='password_reset_confirm'),
url(
r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$',
confirm,
name="account_confirm_email"
),
url(r'^', include('core.urls', namespace='core')),
]
When request to password_reset is posted, user receives email with link contains uid and token. Then user fills new_password in react form, and we want post data:
{
"new_password1": "new_password",
"new_password2": "new_password",
"uid": "",
"token": ""
}
to /rest-auth/password/reset/confirm/.
How we may to obtain this uid and token on frontend for make page with confirm password reset on this url, and then post data for confirm password change?
Registration and forgot password routes We're going to create four URL endpoints: /register that includes the registration form and sends the activation token email . /activate that validates the activation token from the email. /password reset that includes the forgot password form and sends the reset token email .
Here we will use a library called django-rest-passwordreset for creating Reset or Forgot Password API using Django Rest Framework. In models.py add following signal for sending email. Now copy that token which comes in email and and post token and password to /api/password_reset/confirm/ api url.
You should configure your react router to get the params from url. For eg,
<Route path="reset/:uid/:token" component={PasswordResetView}/>
And then in your react view you can get these values like this.props.params.uid
and this.props.params.token
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With