繁体   English   中英

Firebase自定义密码重置链接不起作用

[英]Firebase custom password reset link not working

在过去的两天内,我一直在尝试为我的用户实施自定义密码重置链接,而不是标准的“ .firebaseapp.com”域。 因此,在安装程序中有一个简短的“安装指南”,其中包含我应该在Web服务器上包含的内容的相当小的代码段。

因此,我在根目录上创建了一个新目录,称为“用户”。 然后,我在该目录中创建了一个“ index.php”文件,该文件用于处理密码重置。 最后,我根据文档更新了“ index.php”的代码。 这是什么:

<script src="https://www.gstatic.com/firebasejs/3.4.0/firebase.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
  // TODO: Implement getParameterByName()

  // Get the action to complete.
  var mode = getParameterByName('mode');
  // Get the one-time code from the query parameter.
  var actionCode = getParameterByName('oobCode'};
  // (Optional) Get the API key from the query parameter.
  var apiKey = getParameterByName('apiKey'};

  // Configure the Firebase SDK.
  // This is the minimum configuration required for the API to be used.
  var config = {
    'apiKey': apiKey  // This key could also be copied from the web
                      // initialization snippet found in the Firebase console.
  };
  var app = firebase.initializeApp(config);
  var auth = app.auth();

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

显然,我调整了代码,为我的应用程序提供了有效的apiKey,并更改了两个非常不必要的错误,更改了此操作:

var actionCode = getParameterByName('oobCode'};

var actionCode = getParameterByName('oobCode');

以及:

var apiKey = getParameterByName('apiKey'};

var apiKey = getParameterByName('apiKey');

因此,当我这样做时,我注意到没有函数“ getParamtersByName()”。 在Google搜索之后,我得到了以下代码:

function getParameterByName(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);

    if(results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));

}

因此,在此之后,我意识到文档明显缺少代码。 根据Firebase的介绍,我还需要以下代码段来设置密码重置表单:

function handleResetPassword(auth, actionCode) {
  var accountEmail;
  // Verify the password reset code is valid.
  auth.verifyPasswordResetCode(actionCode).then(function(email) {
    var accountEmail = email;

    // TODO: Show the reset screen with the user's email and ask the user for
    // the new password.

    // Save the new password.
    auth.confirmPasswordReset(actionCode, newPassword).then(function(resp) {
      // Password reset has been confirmed and new password updated.

      // TODO: Display a link back to the app, or sign-in the user directly
      // if the page belongs to the same domain as the app:
      // auth.signInWithEmailAndPassword(accountEmail, newPassword);
    }).catch(function(error) {
      // Error occurred during confirmation. The code might have expired or the
      // password is too weak.
    });
  }).catch(function(error) {
    // Invalid or expired action code. Ask user to try to reset the password
    // again.
  });
}

但是再说一次,在还提供了此代码之后-我的页面仍然空白。 是的,这是因为根本没有代码显示重置密码屏幕。 所以我的问题是; 我将如何完成此任务?

有什么我可以看的项目吗? 我的意思是,必须为此解决方案,因为我怀疑没有人使用自定义电子邮件验证链接。

帮助将不胜感激。

(请注意,我几乎没有使用Firebase上网的经验,JavaScript并不是我都不是非常熟悉的语言,但是我理解它)。

这是您应该放置为firebase自定义密码休息链接的内容。

function sendEmailClick() {
    var auth = firebase.auth();
    var email, photoUrl, uid, emailVerified;
    var email;
    var user = firebase.auth().currentUser;
    var emailAddress = user.email;

    auth.sendPasswordResetEmail(emailAddress).then(function() {
        // Email sent.
    }).catch(function(error) {
        // An error happened.
    });
};

暂无
暂无

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

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