Firebase 身份验证密码重置:如何正确发送包含确认码的密码重置电子邮件?

Ram*_*ani 3 firebase typescript firebase-authentication ionic3 angular

我一直在让 Firebase 发送一封密码重置电子邮件,其中包括该方法所需的确认代码firebase.auth().verifyPasswordResetCode(code)

我正在使用该sendPasswordResetEmail(email)方法,电子邮件中包含一个链接,可让用户创建新密码。

由于我对密码设置了一些限制(它们必须包含一个数字),这将打破限制。

我在文档中找不到任何关于如何将确认码发送到电子邮件以重置密码的内容。

这就是我使用它的方式:

public sendResetPasswordEmail(email: string): Promise<any> {

  return this.fireAgent.firebase.auth().sendPasswordResetEmail(email, {
    url: 'http://localhost:8100/',
    handleCodeInApp: true
  });

}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。

Fra*_*cia 5

据我所知,Firebase 身份验证不允许您指定用户可以在重置密码电子邮件为您提供的网址上插入的密码类型。但是,您应该可以使用验证码身份验证服务来实现它。

要使用验证码,您需要将对象添加ActionCodeSetting到您的sendPasswordResetEmail方法。它应该是这样的:

var actionCodeSettings = {
  // URL you want to redirect back to. The domain (www.example.com) for this
  // URL must be whitelisted in the Firebase Console.
  url: 'https://www.example.com/finishSignUp?cartId=1234',
  // This must be true.
  handleCodeInApp: true,
  iOS: {
    bundleId: 'com.example.ios'
  },
  android: {
    packageName: 'com.example.android',
    installApp: true,
    minimumVersion: '12'
  }
};
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您唯一感兴趣的是handleCodeInApp. 这样,电子邮件将为您提供一个您需要手动输入的代码verifyPasswordResetCode

在此之后,您可以在客户端中手动输入新密码,而无需使用 Firebase 电子邮件提供商。只需confirmPasswordReset("verificationCode", "newPassword")在检查验证码是否有效后致电verifyPasswordResetCode

希望这会帮助你!