【问题标题】:Google Firebase forget passwordGoogle Firebase 忘记密码
【发布时间】:2016-11-04 17:20:41
【问题描述】:

你如何在我的方法中实现忘记密码的方法。我正在创建一个即将到期的 HTML 项目。我的代码:

    function toggleSignIn() {
   if (!firebase.auth().currentUser) {
// [START createprovider]
var provider = new firebase.auth.GoogleAuthProvider();
// [END createprovider]
// [START addscopes]
provider.addScope('https://www.googleapis.com/auth/plus.login');
// [END addscopes]
// [START signin]
firebase.auth().signInWithPopup(provider).then(function(result) {
  // This gives you a Google Access Token. You can use it to access the    Google API.
  var token = result.credential.accessToken;
  // The signed-in user info.
  var user = result.user;
  // [START_EXCLUDE]
  document.getElementById('quickstart-oauthtoken').textContent = token;
  // [END_EXCLUDE]
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // The email of the user's account used.
  var email = error.email;
  // The firebase.auth.AuthCredential type that was used.
  var credential = error.credential; 
  // [START_EXCLUDE]
  if (errorCode === 'auth/account-exists-with-different-credential') {
    alert("You have already signed up with a different auth provider for that email.");
    // If you are using multiple auth providers on your app you should handle linking
    // the user's accounts here.
  }
else if (errorCode === 'auth/auth-domain-config-required') {
alert("An auth domain configuration is required"); 
  }
  else if (errorCode === 'auth/cancelled-popup-request') {
      alert("Popup Google sign in was canceled");
  }
  else if (errorCode === 'auth/operation-not-allowed') {
      alert("Operation is not allowed");
  }
  else if (errorCode === 'auth/operation-not-supported-in-this-environment')      {
      alert("Operation is not supported in this environment");
  }
  else if (errorCode === 'auth/popup-blocked') {
      alert("Sign in popup got blocked");
  }
  else if (errorCode === 'auth/popup-closed-by-user') {
      alert("Google sign in popup got cancelled");
  }
  else if (errorCode === 'auth/unauthorized-domain') {
      alert("Unauthorized domain");
  }
   else {
    console.error(error);
  }
  // [END_EXCLUDE]
  });
  // [END signin]
  } else {
     // [START signout]
    firebase.auth().signOut();
    // [END signout]
    }
   // [START_EXCLUDE]
  document.getElementById('quickstart-sign-ing').disabled = false;
  // [END_EXCLUDE]
 }

以下链接可为您提供指导: https://firebase.google.com/docs/auth/web/manage-users#set_a_users_password 请你帮帮我

【问题讨论】:

    标签: javascript html firebase firebase-authentication


    【解决方案1】:

    要实现忘记密码按钮,您必须调用: firebase.auth().sendPasswordResetEmail('user@example.com')

    查看文档了解更多详情: https://firebase.google.com/docs/reference/js/firebase.auth.Auth#sendPasswordResetEmail

    【讨论】:

    • 你好。我正在使用 firebase auth api 实现忘记密码功能。并使用了 sendPasswordResetEmail() 函数,它运行良好。但是firebase api doc说“要完成密码重置,请使用发送给用户的电子邮件中提供的代码以及用户指定的新密码调用firebase.auth.Auth#confirmPasswordReset。”现在我想知道 confirmPasswordReset () 函数的参数。 confirmPasswordReset(code),这里的code哪里来的?
    • 您必须从密码重置电子邮件链接中解析该代码。用户将单击该页面。如果您托管自己的登录页面,这也是相关的。您必须在 Firebase 控制台中进行修改。
    【解决方案2】:

    实现找回忘记密码功能的最简单方法是调用

    import { AngularFireAuth } from "@angular/fire/auth";
    
    constructor(
        public afAuth: AngularFireAuth
      ) {  }
    
      // Reset Forggot password
      ForgotPassword(passwordResetEmail) {
        return this.afAuth.auth.sendPasswordResetEmail(passwordResetEmail)
        .then(() => {
          window.alert('Password reset email sent, check your inbox.');
        }).catch((error) => {
          window.alert(error)
        })
      }
    

    【讨论】:

    • 根据您链接的博客文章由与您的用户名同名的人撰写,您似乎已链接到您自己的网站/博客。如果您这样做,您必须披露它是您的网站。如果您不透露它是您自己的网站,则会被视为垃圾邮件。请参阅:What signifies "Good" self promotion?self-promotion in the help centerWhat is the exact definition of "spam" for Stack Overflow?What makes something spam
    • 我已删除您在此答案中的链接以及您可能写的博客文章中的其他答案。披露您链接到自己的内容是必需的,否则将被视为垃圾邮件。自从我留下我之前的评论后,您已经访问过 Stack Overflow 几次,既没有添加披露信息,也没有声明您不是博客作者。我的假设是您选择不向您的两个答案添加披露。披露实际上很容易(例如“在我的博客文章中”),所以我假设您更愿意保留答案,但它们不必是垃圾邮件。因此,我删除了这些链接。
    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2017-08-05
    • 2012-07-25
    • 1970-01-01
    • 2012-12-20
    • 2013-05-17
    相关资源
    最近更新 更多