Django-rest-auth 重置密码的 uid 和 token

11

我们的团队在后端使用django-rest-api,在前端使用react开发项目。我们使用django-rest-auth进行身份验证,但是我们在密码重置方面遇到了问题。以下是相关链接:

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')),
]

当用户提交密码重置请求时,系统会发送包含uid和token的链接到用户邮箱。然后用户填写新密码并提交react表单,我们需要提交以下数据:

```python { "new_password": "新密码", "uid": "用户id", "token": "令牌" } ```
{
   "new_password1": "new_password",
   "new_password2": "new_password",
   "uid": "",
   "token": ""
}

访问 /rest-auth/password/reset/confirm/ 页面时,我们需要在前端获取uid和token信息,以便制作确认密码重置页面并提交数据进行密码更改的确认。


你不能直接从页面的URL中获取uid和token吗?这就是Django内置功能类似实现方式。你可以使用JavaScript获取URL。顺便说一下,这是密码重置令牌的实际实现。https://github.com/django/django/blob/master/django/contrib/auth/tokens.py - Håken Lid
1个回答

10

你应该配置你的React路由器以从URL中获取参数。例如


<Route path="reset/:uid/:token" component={PasswordResetView}/>

然后在您的React视图中,您可以像这样获取这些值:this.props.params.uidthis.props.params.token


我正在使用React 16.7和React Router 4.2,我发现我需要在路径前面加上斜杠,并使用this.props.match.params.uidthis.props.match.params.token来获取值。 - pjdavis
我无法理解这个在我的生产网站上是如何工作的。在开发环境中,由于前端和后端位于不同的端口上,因此URL中的域名不同,所以我可以很好地使其正常工作。现在我正在从同一个域运行所有内容,它总是首先选择Django Rest Framework页面,因此我看不到我在React中构建的密码重置页面... - daveyb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接