简体   繁体   English

如何使用 Django Rest Framework 和 Dj-rest-auth 在 React 中显示我的重置密码页面

[英]How to show my reset password page in React using Django Rest Framework and Dj-rest-auth

I have an app that has a React front end and a Django api backend.我有一个应用程序,它有一个 React 前端和一个 Django api 后端。 I'm trying to get the password reset page to work but I can't work out how to set up the urls/routing.我正在尝试让密码重置页面正常工作,但我不知道如何设置 URL/路由。 I had it working locally, but I think that was because the ports for the frontend and backend are different locally, so they were essentially different domains.我让它在本地工作,但我认为这是因为前端和后端的端口在本地不同,所以它们本质上是不同的域。

My Django url patterns includes:我的 Django 网址模式包括:

path('api/v1/dj-rest-auth/password/reset/confirm/<uidb64>/<token>/', PasswordResetConfirmView.as_view(), name='password_reset_confirm')

I have a catchall url in my Django urls to make the rest of the frontend work.我的 Django urls 中有一个包罗万象的 url,以使前端的其余部分工作。

My React Router includes:我的 React 路由器包括:

<Route path="/api/v1/dj-rest-auth/password/reset/confirm/:uid/:token/" component={ResetPassword} />

My React password reset page contains:我的 React 密码重置页面包含:

    let url = process.env.REACT_APP_API_PATH + '/api/v1/dj-rest-auth/password/reset/confirm/' + this.props.match.params.uid + '/' +  this.props.match.params.token + '/';
    let headers = {token: this.props.match.params.token, 
                    uid: this.props.match.params.uid, 
                    new_password1: this.state.newPassword, 
                    new_password2: this.state.newPasswordConf
                  }
    axios
      .post(url, headers)

This takes the token and uid from the password reset link then sends them, along with the user's new password to the password reset endpoint.这从密码重置链接中获取令牌和 uid,然后将它们与用户的新密码一起发送到密码重置端点。

The dilemma seems to be, I need to access the reset link via React to be able to get the token and uid, but then I need to access the same link as a call to the API to be able to reset the password.困境似乎是,我需要通过 React 访问重置链接才能获取令牌和 uid,但随后我需要访问与调用 API 相同的链接才能重置密码。

OK, I managed to solve it using a slightly cheeky way.好的,我设法用一种有点厚颜无耻的方式解决了它。

I simply overrode the template for the password reset email, using the default template, but just adding something extra to the url.我只是使用默认模板覆盖了密码重置电子邮件的模板,但只是在 url 中添加了一些额外的东西。 Just so the url is different in some way.只是网址在某些方面有所不同。

I copied this template to templates/registration将此模板复制到templates/registration

Then just added an extra word ( email - but could be any word) to the path that gets created in the email:然后在电子邮件中创建的路径中添加一个额外的词( email - 但可以是任何词):

{{ protocol }}://{{ domain }}/email{% url 'password_reset_confirm' uidb64=uid token=token %}

Now I can use this new path in my my React Router to get the uid and token, but when I submit the data to the api I use the 'proper' url (the one in my Django urlpatterns)现在我可以在我的 React 路由器中使用这个新路径来获取 uid 和令牌,但是当我将数据提交到 api 时,我使用了“正确”的 url(我的 Django urlpatterns 中的那个)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM