简体   繁体   English

Firebase 身份验证 - 忘记/重设密码

[英]Firebase Authentication - Forget/ Reset Password

I am adding the functionality of authentication to my app.我正在向我的应用程序添加身份验证功能。 I am using firebase REST api for authentication.我正在使用 firebase REST api进行身份验证。 I am successful in implementing login, logout, signup etc functions.我成功地实现了登录、注销、注册等功能。 Now I want to implement forget or reset password.现在我想实现忘记或重设密码。

I want to send the api request to firebase to generate the code and receive it at user's email. So that app can send back the code with the new password back to firebase for resetting.我想将 api 请求发送到 firebase 以生成代码并在用户的 email 接收它。这样应用程序可以将带有新密码的代码发回 firebase 以进行重置。

I am not able to find the appropriate api from firebase documentation.我无法从 firebase 文档中找到合适的 api。 Will anyone please advise?有人会建议吗?

I'm right in the middle of working with password changing with Firebase myself, and though I'm not using Angular, here's the links to the two key methods needed for changing the password, from the FB Auth documentation.我自己正在使用 Firebase 更改密码,虽然我没有使用 Angular,但这里是更改密码所需的两个关键方法的链接,来自 FB Auth 文档。

From the docs, to send the email: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#sendpasswordresetemail从文档中发送 email: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#sendpasswordresetemail

import { getAuth, sendPasswordResetEmail } from "firebase/auth";

const auth = getAuth();
sendPasswordResetEmail(auth, email)
  .then(() => {
    // Password reset email sent!
    // ..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });

Once the user clicks the link in the email, they will end up on your site's email landing page (defined in the Auth email templates or through your custom third party email solution).一旦用户单击 email 中的链接,他们将最终到达您网站的 email 登录页面(在 Auth email 模板中或通过您的自定义第三方 email 解决方案定义)。 The link will have a parameter "oobCode", which is needed to reset the password.该链接将有一个参数“oobCode”,需要它来重置密码。

On that landing page, ask the user for a new password, and then pass the oobCode and the new password to the confirmPasswordReset function: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth#confirmpasswordreset在该着陆页上,要求用户输入新密码,然后将 oobCode 和新密码传递给confirmPasswordReset function: https://firebase.google.com/docs/reference/js/v8/firebase.auth.Auth #确认密码重置

import { getAuth, confirmPasswordReset } from "firebase/auth";

const auth = getAuth();
const oobCode = [However Angular reads querystring values]('oobCode');
confirmPasswordReset (auth, oobCode)
  .then(() => {
    // Password has been reset!
    // ..
  })
  .catch((error) => {
    const errorCode = error.code;
    const errorMessage = error.message;
    // ..
  });

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

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