NodeJS 如何使用Firebase auth自动发送密码重置电子邮件

eanckbw9  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(138)

我有一个登录页面,人们可以使用Firebase Auth登录,但我没有做一个无法登录的组件,如果他们忘记了密码,就会发生密码重置,并将新密码通过电子邮件发送给用户。
我已经阅读了有关此的文档,在firebase文档的生成电子邮件操作链接部分的生成密码重置电子邮件链接中,在authentification下,它向我展示了node.js的代码。

// Admin SDK API to generate the password reset link.
const userEmail = 'user@example.com';
getAuth()
  .generatePasswordResetLink(userEmail, actionCodeSettings)
  .then((link) => {
    // Construct password reset email template, embed the link and send
    // using custom SMTP server.
    return sendCustomPasswordResetEmail(userEmail, displayName, link);
  })
  .catch((error) => {
    // Some error occurred.
  });

我也看过关于创建自定义电子邮件操作处理程序的文档,并看到了这一点

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

document.addEventListener('DOMContentLoaded', () => {
  // TODO: Implement getParameterByName()

  // Get the action to complete.
  const mode = getParameterByName('mode');
  // Get the one-time code from the query parameter.
  const actionCode = getParameterByName('oobCode');
  // (Optional) Get the continue URL from the query parameter if available.
  const continueUrl = getParameterByName('continueUrl');
  // (Optional) Get the language code if available.
  const lang = getParameterByName('lang') || 'en';

  // Configure the Firebase SDK.
  // This is the minimum configuration required for the API to be used.
  const config = {
    'apiKey': "YOU_API_KEY" // Copy this key from the web initialization
                            // snippet found in the Firebase console.
  };
  const app = initializeApp(config);
  const auth = getAuth(app);

  // Handle the user management action.
  switch (mode) {
    case 'resetPassword':
      // Display reset password handler and UI.
      handleResetPassword(auth, actionCode, continueUrl, lang);
      break;
    case 'recoverEmail':
      // Display email recovery handler and UI.
      handleRecoverEmail(auth, actionCode, lang);
      break;
    case 'verifyEmail':
      // Display email verification handler and UI.
      handleVerifyEmail(auth, actionCode, continueUrl, lang);
      break;
    default:
      // Error: invalid mode.
  }
}, false);

我知道这可能是一个简单的问题,但我对此非常陌生,我一直无法找到任何好的教程视频,我希望这里的人可以向我展示如何做到这一点的例子,或者解释它的某个地方的链接,以便即使我可以理解,任何帮助都将非常感谢。

k3bvogb1

k3bvogb11#

您的问题(源代码)中的第一个代码片段是Admin SDK代码,因此需要从“特权环境”中执行,如doc中所述。
在文档中,“特权环境”指的是您完全控制的服务器Firebase项目中运行后端代码的云函数(并且您也可以控制,因为它在您自己的项目中)。因此,此代码不应在您的前端应用程序中执行。
最简单的方法是使用一个Cloud Function,您可以根据您的功能需求通过不同的方式触发它,例如:通过calling it from your app,通过creating a Firestore document,等等……
如代码段中所述

// Construct password reset email template, embed the link and send
// using custom SMTP server.
return sendCustomPasswordResetEmail(userEmail, displayName, link);

您需要向自己发送电子邮件,包括生成电子邮件内容(通过包含generatePasswordResetLink()方法返回的链接)并使用自己的SMTP服务器或Sendgrid等服务发送。这段代码中的sendCustomPasswordResetEmail()函数只是一个“假”函数,它说明了需要实现我上一句话中描述的操作的事实。
然后,您的问题中的第二个代码片段将在用户的浏览器中执行。收到电子邮件后,用户单击链接,您应该向用户展示一个实现documentation where you got this second code snippet中详细说明的handleResetPassword()函数中的代码的网页(参见第2节/要点)。
请注意,第二个代码片段处理所有不同的电子邮件操作处理程序,即重置密码,还可以撤销电子邮件地址更改或验证电子邮件地址。在你的例子中,你只需要实现handleResetPassword()函数中的代码。

相关问题