Skip to content

Commit

Permalink
fix: i like potatoes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kan-A-Pesh committed Nov 30, 2024
1 parent 70242ea commit 2465c9d
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions routes/auth/send-mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ export default async function Route_Auth_Sendmail(req: Request, res: Response, n
});
}

if (globals.env.NODE_ENV === "production" && !isAdmin(req)) {
if (/^[a-zA-Z0-9._%+-]+@edu\.devinci\.fr$/.test(bodyPayload.data.email) === false) {
const parsedEmail = bodyPayload.data.email.toLowerCase().replace(/\./g, "").replace(/\+.*@/g, "@");

if (!isAdmin(req)) {
if (/^[a-zA-Z0-9._%-]+@edu\.devinci\.fr$/.test(parsedEmail) === false) {
return Status.send(req, next, {
status: 400,
error: "errors.auth.invalid.email"
Expand All @@ -55,27 +57,25 @@ export default async function Route_Auth_Sendmail(req: Request, res: Response, n
);
}

if (await Redis.get(`timeout::${bodyPayload.data.email}`)) {
if (await Redis.get(`timeout::${parsedEmail}`)) {
return Status.send(req, next, {
status: 429,
error: "errors.auth.toomany"
});
}

if (await UserController.existsUserByEmail(bodyPayload.data.email)) {
if (await UserController.existsUserByEmail(parsedEmail)) {
return Status.send(req, next, {
status: 409,
error: "errors.auth.conflict.email"
});
}

const creationToken = AuthController.generateCreationToken(bodyPayload.data.email, !isAdmin(req));
const creationToken = AuthController.generateCreationToken(parsedEmail, !isAdmin(req));
const creationUrl = globals.env.MAIL_REDIRECT_URL.replace("{token}", creationToken);

try {
Logger.debug(
`send-mail.ts::Route_Auth_Sendmail: Sending email to "${bodyPayload.data.email}" with link "${creationUrl}"`
);
Logger.debug(`send-mail.ts::Route_Auth_Sendmail: Sending email to "${parsedEmail}" with link "${creationUrl}"`);

// Render the email template
const emailHtml = await render(EmailTemplate({ baseUrl: globals.env.MAIL_ASSETS_URL, magicLink: creationUrl }));
Expand All @@ -84,10 +84,10 @@ export default async function Route_Auth_Sendmail(req: Request, res: Response, n
if (isAdmin(req)) {
Logger.debug(`send-mail.ts::Route_Auth_Sendmail: Skipping email sending (as admin)`);
} else {
await sendEmail(emailHtml, bodyPayload.data.email);
await sendEmail(emailHtml, parsedEmail);
}

Redis.set(`timeout::${bodyPayload.data.email}`, true, 50);
Redis.set(`timeout::${parsedEmail}`, true, 50);
} catch (e) {
Logger.error("send-mail.ts::Route_Auth_Sendmail: Error while sending email", e);
return Status.send(req, next, {
Expand Down

0 comments on commit 2465c9d

Please sign in to comment.