in src/app/api/v1/user/resend-email/route.ts [22:79]
export async function POST(req: NextRequest) {
const token = await getToken({ req });
const l10n = getL10n(await getAcceptLangHeaderInServerComponents());
if (typeof token?.subscriber?.fxa_uid === "string") {
try {
const { emailId }: EmailResendRequest = await req.json();
const parsedEmailId = Number.parseInt(emailId, 10);
if (Number.isNaN(parsedEmailId)) {
throw new Error("No valid email address given.");
}
const subscriber = await getSubscriberByFxaUid(token.subscriber?.fxa_uid);
if (!subscriber) {
throw new Error("No subscriber found for current session.");
}
const existingEmail = await getUserEmails(subscriber.id);
const filteredEmail = existingEmail.filter(
(a) => a.email === emailId && a.subscriber_id === subscriber.id,
);
if (!filteredEmail) {
return NextResponse.json(
{
success: false,
message: l10n.getString("user-verify-token-error"),
},
{ status: 500 },
);
}
await initEmail();
await sendVerificationEmail(subscriber, parsedEmailId);
return NextResponse.json({
success: true,
message: "Sent the verification email",
});
} catch (e) {
logger.error(e);
if (
e instanceof Error &&
e.message === "error-email-validation-pending"
) {
return NextResponse.json(
{
success: false,
message: "Verification email recently sent, try again later",
},
{ status: 429 },
);
}
return NextResponse.json({ success: false }, { status: 500 });
}
} else {
return NextResponse.json({ success: false }, { status: 401 });
}
}