I have integrated
"firebase": "^10.13.0"
for user authentication in my project using next.js 14.2
along with typescript
.
However, I am encountering an issue where updating the user password results in the auth/requires-recent-login
error even after reauthentication.
My assumption was that after successful reauthentication, the user would have a grace period to update their password before being prompted to login again due to it being "too long ago."
To mitigate this, I have attempted to directly invoke the updatePassword function post reauthentication, but unfortunately, the problem persists.
Outlined below is the workflow:
updateUserPassword
:
function updateUserPassword(password: string): void {
const auth = getAuth();
const user = auth.currentUser;
if (user) {
updatePassword(user, password)
.then(() => {
console.log("Password updated!");
})
.catch((error: Error) => {
console.log("Error updateUserPassword: " + error.message);
if (error.message.includes("auth/requires-recent-login")) {
console.log("reauthUser");
reauthUser(password);
}
});
} else {
console.log("User is not authenticated");
}
}
reauthUser
:
const reauthUser = async (password: string): Promise<void> => {
const auth = getAuth();
if (auth.currentUser) {
const providerId = auth.currentUser.providerData[0]?.providerId;
console.log("providerId: ", providerId);
if (providerId === "google.com") {
await reauthWithGoogle();
updatePassword(auth.currentUser, password)
.then(() => {
console.log("Password updated!");
})
.catch((error: Error) => {
console.log(
"Error updateUserPassword in reauthUser: " + error.message,
);
});
} else {
alert("Unsupported authentication provider.");
}
} else {
router.push(ROUTES.signin);
}
};
reauthWithGoogle
const reauthWithGoogle = async (): Promise<void> => {
const auth = getAuth();
googleProvider.setCustomParameters({ prompt: "select_account" });
if (auth.currentUser) {
reauthenticateWithPopup(auth.currentUser, googleProvider)
.then(async (result) => {
console.log("reauthenticateWithPopup");
})
.catch((error) => {
alert("Error reauthenticateWithPopup: " + error.message);
});
} else {
router.push(ROUTES.signin);
}
};