Firebase自定义密码重置链接无效

时间:2016-10-22 20:51:45

标签: javascript firebase firebase-authentication

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

因此我在我的root上创建了一个名为“users”的新目录。然后我在该目录中创建了一个'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 for web方面的经验很少,JavaScript也不是我非常熟悉的语言,但我理解它。)

1 个答案:

答案 0 :(得分:0)

这是您应该将其作为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.
    });
};