【问题标题】:NextAuth.js - Credentials authentication, adding reset password buttonNextAuth.js - 凭据身份验证,添加重置密码按钮
【发布时间】:2021-05-26 04:08:15
【问题描述】:

我正在使用 next/auth 开发登录系统,并使用仅限邀请的系统实现凭据记录系统。有没有办法将重置密码链接添加到生成的/api/auth/signin 页面?

【问题讨论】:

    标签: reactjs next.js next-auth


    【解决方案1】:

    您最好创建自己的自定义登录页面 (https://next-auth.js.org/configuration/pages),然后您可以在其中添加您想要的“重置密码”功能

    【讨论】:

      【解决方案2】:

      我看到并使用了与此类似的那个。也许这可以帮助你。

      • 从我的后端发送一个帖子来创建一个新的 auth0 用户。此时auth0 user.email_verified = false。
      • 发送帖子以触发新用户的密码重置电子邮件。
      {% if user.email_verified == false %}
                  <h1>Invitation to our awesome app</h1>
      
                  <p>Please verify your email address and set your initial password by clicking the following link:</p>
      
                  <p><a href="{{ url }}">Confirm my account</a></p>
      
        {% else %}
      
                  <h1>Password Change Request</h1>
      
                  <p>You have submitted a password change request. </p>
      
                  <p>If it wasn't you please disregard this email and make sure you can still login to your account. If it was you, then to <strong>confirm the password change <a href="{{ url }}">click here</a></strong>.</p>
      
          {% endif %}
      
                  <p>If you have any issues with your account, please don’t hesitate to contact us at 1-888-AWESOMECO.</p>
      
                  <br>
                  Thanks!
                  <br>
      
      • 在密码重置电子邮件模板上配置重定向,以便当用户单击邀请链接时,系统会提示他们重置密码,然后他们将被重定向到我们的应用程序,然后应用程序会要求他们登录
      • 我添加了一个 auth0 规则,以在首次登录/密码重置时设置 email_verified = true(这是预设选项之一)
       }
      
        if (user.email_verified || !user.last_password_reset) {
          return callback(null, user, context);
        }
      
        // Set email verified if a user has already updated his/her password
        request.patch({
          url: userApiUrl + user.user_id,
          headers: {
            Authorization: 'Bearer ' + auth0.accessToken
          },
          json: { email_verified: true },
          timeout: 5000
        },
        function(err, response, body) {
          // Setting email verified isn't propagated to id_token in this
          // authentication cycle so explicitly set it to true given no errors.
          context.idToken.email_verified = (!err && response.statusCode === 200);
      
          // Return with success at this point.
          return callback(null, user, context);
        });
      }
      
      • 下次我们需要向他们发送密码重置电子邮件时,它将使用模板的“现有用户”风格

      • 邀请电子邮件密码重置链接具有可配置的 TTL - 默认为 5 天。因此,如果他们不接受邀请,它最终会超时(如果需要,我们可以向他们发送另一个)

      【讨论】:

        猜你喜欢
        • 2010-10-03
        • 1970-01-01
        • 2021-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 2016-07-21
        相关资源
        最近更新 更多