( req: MonkeyRequest, )
| 142 | } |
| 143 | |
| 144 | export async function sendVerificationEmail( |
| 145 | req: MonkeyRequest, |
| 146 | ): Promise<MonkeyResponse> { |
| 147 | const { email, uid } = req.ctx.decodedToken; |
| 148 | const isVerified = ( |
| 149 | await FirebaseAdmin() |
| 150 | .auth() |
| 151 | .getUser(uid) |
| 152 | .catch((e: unknown) => { |
| 153 | throw new MonkeyError( |
| 154 | 500, // this should never happen, but it does. it mightve been caused by auth token cache, will see if disabling cache fixes it |
| 155 | "Auth user not found, even though the token got decoded", |
| 156 | JSON.stringify({ |
| 157 | uid, |
| 158 | email, |
| 159 | stack: e instanceof Error ? e.stack : JSON.stringify(e), |
| 160 | }), |
| 161 | uid, |
| 162 | ); |
| 163 | }) |
| 164 | ).emailVerified; |
| 165 | if (isVerified) { |
| 166 | throw new MonkeyError(400, "Email already verified"); |
| 167 | } |
| 168 | |
| 169 | const userInfo = await UserDAL.getPartialUser( |
| 170 | uid, |
| 171 | "request verification email", |
| 172 | ["uid", "name", "email"], |
| 173 | ); |
| 174 | |
| 175 | if (userInfo.email !== email) { |
| 176 | throw new MonkeyError( |
| 177 | 400, |
| 178 | "Authenticated email does not match the email found in the database. This might happen if you recently changed your email. Please refresh and try again.", |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | const { data: link, error } = await tryCatch( |
| 183 | FirebaseAdmin() |
| 184 | .auth() |
| 185 | .generateEmailVerificationLink(email, { url: getFrontendUrl() }), |
| 186 | ); |
| 187 | |
| 188 | if (error) { |
| 189 | if (isFirebaseError(error)) { |
| 190 | if (error.errorInfo.code === "auth/user-not-found") { |
| 191 | throw new MonkeyError( |
| 192 | 500, |
| 193 | "Auth user not found when the user was found in the database. Contact support with this error message and your email", |
| 194 | JSON.stringify({ |
| 195 | decodedTokenEmail: email, |
| 196 | userInfoEmail: userInfo.email, |
| 197 | }), |
| 198 | userInfo.uid, |
| 199 | ); |
| 200 | } else if (error.errorInfo.code === "auth/too-many-requests") { |
| 201 | throw new MonkeyError(429, "Too many requests. Please try again later"); |
nothing calls this directly
no test coverage detected