impr(server): move password update from client to server

this allows us to make sure to invalidate user tokens on password change

!nuf
This commit is contained in:
Miodec 2024-05-24 14:34:58 +02:00
parent 313468cee3
commit 8e057e3cb6
5 changed files with 46 additions and 6 deletions

View file

@ -360,6 +360,18 @@ export async function updateEmail(
return new MonkeyResponse("Email updated");
}
export async function updatePassword(
req: MonkeyTypes.Request
): Promise<MonkeyResponse> {
const { uid } = req.ctx.decodedToken;
const { newPassword } = req.body;
await AuthUtil.updateUserPassword(uid, newPassword);
await AuthUtil.revokeTokensByUid(uid);
return new MonkeyResponse("Password updated");
}
function getRelevantUserInfo(
user: MonkeyTypes.DBUser
): Partial<MonkeyTypes.DBUser> {

View file

@ -211,6 +211,20 @@ router.patch(
asyncHandler(UserController.updateEmail)
);
router.patch(
"/password",
authenticateRequest({
requireFreshToken: true,
}),
RateLimit.userUpdateEmail,
validateRequest({
body: {
newPassword: joi.string().required(),
},
}),
asyncHandler(UserController.updatePassword)
);
router.delete(
"/personalBests",
authenticateRequest({

View file

@ -59,6 +59,15 @@ export async function updateUserEmail(
});
}
export async function updateUserPassword(
uid: string,
password: string
): Promise<UserRecord> {
return await FirebaseAdmin().auth().updateUser(uid, {
password,
});
}
export async function deleteUser(uid: string): Promise<void> {
await FirebaseAdmin().auth().deleteUser(uid);
}

View file

@ -78,6 +78,12 @@ export default class Users {
return await this.httpClient.patch(`${BASE_PATH}/email`, { payload });
}
async updatePassword(newPassword: string): Ape.EndpointResponse<null> {
return await this.httpClient.patch(`${BASE_PATH}/password`, {
payload: { newPassword },
});
}
async deletePersonalBests(): Ape.EndpointResponse<null> {
return await this.httpClient.delete(`${BASE_PATH}/personalBests`);
}

View file

@ -811,17 +811,16 @@ list.updatePassword = new SimpleModal({
};
}
try {
await updatePassword(reauth.user, newPass);
} catch (e) {
const message = createErrorMessage(e, "Failed to update password");
const response = await Ape.users.updatePassword(newPass);
if (response.status !== 200) {
return {
status: -1,
message,
message: "Failed to update password: " + response.message,
};
}
reloadAfter(3);
AccountController.signOut();
return {
status: 1,