( req: MonkeyRequest<undefined, UpdateEmailRequest>, )
| 445 | } |
| 446 | |
| 447 | export async function updateEmail( |
| 448 | req: MonkeyRequest<undefined, UpdateEmailRequest>, |
| 449 | ): Promise<MonkeyResponse> { |
| 450 | const { uid } = req.ctx.decodedToken; |
| 451 | let { newEmail, previousEmail } = req.body; |
| 452 | |
| 453 | newEmail = newEmail.toLowerCase(); |
| 454 | previousEmail = previousEmail.toLowerCase(); |
| 455 | |
| 456 | try { |
| 457 | await AuthUtil.updateUserEmail(uid, newEmail); |
| 458 | await UserDAL.updateEmail(uid, newEmail); |
| 459 | } catch (e) { |
| 460 | if (isFirebaseError(e)) { |
| 461 | if (e.code === "auth/email-already-exists") { |
| 462 | throw new MonkeyError( |
| 463 | 409, |
| 464 | "The email address is already in use by another account", |
| 465 | ); |
| 466 | } else if (e.code === "auth/invalid-email") { |
| 467 | throw new MonkeyError(400, "Invalid email address"); |
| 468 | } else if (e.code === "auth/too-many-requests") { |
| 469 | throw new MonkeyError(429, "Too many requests. Please try again later"); |
| 470 | } else if (e.code === "auth/user-not-found") { |
| 471 | throw new MonkeyError( |
| 472 | 404, |
| 473 | "User not found in the auth system", |
| 474 | "update email", |
| 475 | uid, |
| 476 | ); |
| 477 | } else if (e.code === "auth/invalid-user-token") { |
| 478 | throw new MonkeyError(401, "Invalid user token", "update email", uid); |
| 479 | } |
| 480 | } else { |
| 481 | throw e; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | void addImportantLog( |
| 486 | "user_email_updated", |
| 487 | `changed email from ${previousEmail} to ${newEmail}`, |
| 488 | uid, |
| 489 | ); |
| 490 | |
| 491 | return new MonkeyResponse("Email updated", null); |
| 492 | } |
| 493 | |
| 494 | export async function updatePassword( |
| 495 | req: MonkeyRequest<undefined, UpdatePasswordRequest>, |
nothing calls this directly
no test coverage detected