簡體   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