简体   繁体   English

Djoser 密码重置实现

[英]Djoser password reset implementation

I am using djosers for my authentication on django backend which eventually i'll be connecting to flutter frontend and i am having trouble implementing the password reset functionality... from what i have understood, first i need to hit the /users/reset_password/ with email body which will eventually give me the token of authentication which will be used further on confirm reset but the first thing i dont understand is PASSWORD_RESET_CONFIRM_URL field in the settings, like it needs a front end link with uid and token placeholders but what is this token field and what is this PASSWORD_RESET_CONFIRM_URL but i managed to look over a stack overflow question and filled it but now when i hit /users/reset_password/ i get this error:我在 django 后端使用 djosers 进行身份验证,最终我将连接到 flutter 前端,但在实现密码重置功能时遇到了麻烦……据我所知,首先我需要点击 /users/reset_password/带有电子邮件正文,最终会给我身份验证令牌,该令牌将在确认重置时进一步使用,但我不明白的第一件事是设置中的PASSWORD_RESET_CONFIRM_URL字段,就像它需要带有 uid 和令牌占位符的前端链接,但这是什么令牌字段以及这个PASSWORD_RESET_CONFIRM_URL是什么,但我设法查看了堆栈溢出问题并填写了它,但是现在当我点击/users/reset_password/此错误:

[WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

settings:设置:

    DJOSER = {
        'PASSWORD_RESET_CONFIRM_URL':'reset/password/reset/confirm/{uid}/{token}',
        'LOGIN_FIELD' : 'email',
        'USER_CREATE_PASSWORD_RETYPE' : True,
        'SERIALIZERS': {
            'user_create': 'auth_app.serializers.UseriCreateSerializer',
            'user': 'auth_app.serializers.UserCreateSerializer',
        }
    }

urls.py:网址.py:


    urlpatterns = [
        path('',home,name='home'),
        path('addInForum/',addInForum,name='addInForum'),
        path('addInDiscussion/',addInDiscussion,name='addInDiscussion'),
        path('<str:forum_id>/getDiscussion/',getDiscussion,name='getDiscussion'),
        path('getDate/',getDate,name='getDate'),
        path('reset/password/reset/confirm/<str:uid>/<str:token>/',PasswordResetView,name='PasswordResetView'),
       # url(r'^reset/password/reset/confirm/(?P<uid>[\w-]+)/(?P<token>[\w-]+)/$', PasswordResetView.as_view(),),
    ]

views.py视图.py

    @api_view(['GET'])
    def PasswordResetView(request,uid,token):
        post_data = {'uid': uid, 'token': token}
        return Response(post_data)

Please remember that djoser should be part of your REST API based on Django REST Framework.请记住,djoser 应该是基于 Django REST 框架的 REST API 的一部分。 You also need to think differently about the url routing in regard of your frontend app..您还需要对前端应用程序的 url 路由进行不同的思考。

Usually urls in the form mydomain.com/some_url/whatever are considered "frontend urls" and parsed by routing of your frontend app.通常形式为mydomain.com/some_url/whatever url 被视为“前端 url”,并通过前端应用程序的路由进行解析。 On the other hand urls in the form mydomain.com/api/something are considered API urls that are routed via Django's urls.py .另一方面,形式为mydomain.com/api/something的 url 被认为是通过 Django 的urls.py路由的 API url。 I will refer to them as Fronted_URL and API_URL respectively.我将它们分别称为 Fronted_URL 和 API_URL。

So: resetting password works like this.所以:重置密码的工作原理是这样的。 The user that forgot their password and wants to reset it, surely needs to fill some king of form.忘记密码并想重置密码的用户肯定需要填写一些表格。 This form needs to be sent to APIURL returned by resolve('user-reset-password') (by default this returns something like /users/reset_password/ )此表单需要发送到由resolve('user-reset-password')返回的/users/reset_password/ (默认情况下,它返回类似/users/reset_password/

Here comes PASSWORD_RESET_CONFIRM_URL setting.这里是PASSWORD_RESET_CONFIRM_URL设置。 Because after the body is accepted by the APIURL mentioned above, a mail will be sent to the user with a link that will point to URL entered in that setting.因为在上述 APIURL 接受正文后,将向用户发送一封邮件,其中包含指向在该设置中输入的 URL 的链接。 And it has to be FrontendURL!它必须是 FrontendURL! It should be routed by your frontend APP and preferably display some screen.它应该由您的前端 APP 路由,最好显示一些屏幕。 But in the background your frontend app should send the values of uid and token fields to APIURL returned by resolve("user-reset-password-confirm") .但是在后台,您的前端应用程序应该将uidtoken字段的值发送到由resolve("user-reset-password-confirm")返回的 APIURL。

This flow allows your frontend app to properly handle the response and display appropriate message to the user and then maybe redirect them to some other screen.此流程允许您的前端应用程序正确处理响应并向用户显示适当的消息,然后可能会将他们重定向到其他屏幕。

If you don't have a routed frontend app (probably written using REACT, ANGULAR or VUE) then you probably don't need a REST API and should just stick to django-allauth .如果您没有路由前端应用程序(可能是使用 REACT、ANGULAR 或 VUE 编写的),那么您可能不需要 REST API,而应该坚持使用django-allauth

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

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