我正在實作一個云功能來更新當前用戶的密碼。
基本上,我要遵循的邏輯是:
(Client side)
0. Complete form and submit the data (current password and new password).
(Backend)
1. Get the current user email from the callable function context.
2. Re-authenticate the current user using the provided current password.
2.1. If success, change the password and send a notification email.
2.2. Else, throw an error.
這是我當前的代碼:
const { auth, functions } = require("../../services/firebase");
...
exports.updatePassword = functions
.region("us-central1")
.runWith({ memory: "1GB", timeoutSeconds: 120 })
.https.onCall(async (data, context) => {
const { currentPassowrd, newPassword } = data;
const { email, uid: userId } = context.auth.token;
if (!userId) {
// throw ...
}
try {
//
// Problem: `firebase-admin` authentication doesn't include
// the `signInWithEmailAndPassword()` method...
//
await auth.signInWithEmailAndPassword(email, currentPassowrd);
await auth.updateUser(userId, {
password: newPassword,
});
sendPasswordUpdateEmail(email);
} catch (err) {
// ...
throw AuthErrors.cannotUpdatePassword();
}
});
我的問題是firebase-admin包不包含signInWithEmailAndPassword,我需要一種方法來處理這個問題,在我的函式中檢查“currentPassword”是否正確。
我的另一種選擇,如果我描述的那個是不可能的,是在客戶端使用 firebase sdk 更新密碼,然后呼叫 firebase 函式來發送通知電子郵件。
uj5u.com熱心網友回復:
嚴格來說,您不需要在 Cloud Function 中重新對用戶進行身份驗證:如果您context.auth.uid在 Callable Cloud Function 中獲取了updateUser()值.
如果您想處理用戶打開設備并且有人更新密碼的情況,如您問題下的評論中所述,我建議您使用reauthenticateWithCredential()前端的方法,重新驗證用戶使用新的憑據。
執行以下操作:
import {
EmailAuthProvider,
getAuth,
reauthenticateWithCredential,
} from 'firebase/auth'
const email = auth.currentUser.email;
// Capture the password value
// e.g. via a pop-up window
const password = ...;
const auth = getAuth();
const credential = EmailAuthProvider.credential(
email,
password
);
await reauthenticateWithCredential(
auth.currentUser,
credential
);
// If no error is thrown, you can call the Callable Cloud Function, knowing the user has just re-signed-in.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/466774.html
標籤:javascript 节点.js 火力基地 firebase 身份验证 谷歌云功能
