我正在撰寫一個應用程式,我想讓用戶可以更改他們的密碼。所以我有一個簡單的UpdatePassword.js頁面,我在其中呼叫 Firebase 身份驗證.updatePassword(password)方法。如檔案中所述,這是一項敏感操作,因此,用戶需要進行身份驗證(如果他們最近沒有進行身份驗證),以便將密碼更改為新密碼。
這是我的方法:
const update = async () => {
const user = await firebase.auth().currentUser;
await user
.updatePassword(password)
.then(() => {
setUpdated(true);
})
.catch((error) => {
//I want to handle this specific error but I don't know how
if (
error.message ===
"This operation is sensitive and requires recent authentication. Log in again before retrying this request."
) {
console.log("should display a modal for user to authenticate again");
}
console.log("error while updating pass: ", error);
setSaving(false);
});
};
正如您從我console.log的 s 中看到的,在用戶需要再次進行身份驗證的情況下,我想顯示一個模式,他們將在其中再次使用他們的憑據登錄。這不是問題,而且很容易做到。但是,我的問題是,如何在用戶需要進行身份驗證的情況下捕獲這種特定型別的錯誤?根據我console.log的說法,我現在實作它的方式,我只是比較我收到的錯誤訊息Firebase Authentication,這真的不是正確的方法。如果 Firebase Auth 將錯誤訊息更改為其他內容怎么辦?是否有類似錯誤代碼的東西,我可以將其與引發的錯誤進行比較,并通過錯誤代碼或比字串訊息更安全的東西處理例外?
uj5u.com熱心網友回復:
正如您將在檔案中看到的那樣,在這種情況下引發的錯誤(即“如果用戶的上次登錄時間未達到安全閾值”)具有auth/requires-recent-login錯誤代碼。
所以:
//...
.catch((error) => {
if (error.code === 'auth/requires-recent-login') {
// Display the modal
} else {
// ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/505620.html
標籤:Google Cloud Collective javascript 火力基地 firebase 身份验证
下一篇:如何使用props作為組件?
