From faced0b20cb0597d3463ea0424cf912f353a4f1f Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Sun, 22 Oct 2023 09:38:29 +0100 Subject: [PATCH] Replaced the previous e-mail verification process for our own --- src/email/index.ts | 4 +-- src/email/templates/main.handlebars | 2 +- src/email/templates/verification.handlebars | 22 +++++++++++++++ .../templates/verification.handlebars.json | 5 ++++ src/pages/api/reset/sendVerification.ts | 28 +++++++++++-------- src/pages/login.tsx | 3 ++ src/pages/register.tsx | 3 ++ 7 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 src/email/templates/verification.handlebars create mode 100644 src/email/templates/verification.handlebars.json diff --git a/src/email/index.ts b/src/email/index.ts index be8f7e2d..0fd6bff6 100644 --- a/src/email/index.ts +++ b/src/email/index.ts @@ -10,7 +10,7 @@ interface MailOptions { context: object; } -export function prepareMailer(): nodemailer.Transporter { +export function prepareMailer(template?: string): nodemailer.Transporter { const transport = nodemailer.createTransport({ host: process.env.SMTP_HOST, auth: { @@ -22,7 +22,7 @@ export function prepareMailer(): nodemailer.Transporter { const handlebarOptions: hbs.NodemailerExpressHandlebarsOptions = { viewEngine: { partialsDir: path.resolve("src/email/templates"), - defaultLayout: "src/email/templates/main", + defaultLayout: `src/email/templates/${template || "main"}`, }, viewPath: path.resolve("src/email/templates"), }; diff --git a/src/email/templates/main.handlebars b/src/email/templates/main.handlebars index 29f969db..c7ac02a1 100644 --- a/src/email/templates/main.handlebars +++ b/src/email/templates/main.handlebars @@ -7,7 +7,7 @@
+ class="h-full min-h-screen w-full flex flex-col p-8 gap-16 text-base">
Hello future {{type}} of EnCoach,
diff --git a/src/email/templates/verification.handlebars b/src/email/templates/verification.handlebars new file mode 100644 index 00000000..ebf387db --- /dev/null +++ b/src/email/templates/verification.handlebars @@ -0,0 +1,22 @@ + + + + + + + + +
+

Hello {{name}},

+
+

Follow this link to verify your email address.

+ Verify account +
+
+

If you didn’t ask to verify this address, you can ignore this email.

+
+

Thanks,

+

Your EnCoach team

+
+ + \ No newline at end of file diff --git a/src/email/templates/verification.handlebars.json b/src/email/templates/verification.handlebars.json new file mode 100644 index 00000000..2fbb1fad --- /dev/null +++ b/src/email/templates/verification.handlebars.json @@ -0,0 +1,5 @@ +{ + "name": "Tiago Ribeiro", + "email": "tiago.ribeiro@ecrop.dev", + "code": "123" +} \ No newline at end of file diff --git a/src/pages/api/reset/sendVerification.ts b/src/pages/api/reset/sendVerification.ts index e04841a7..7aff44d8 100644 --- a/src/pages/api/reset/sendVerification.ts +++ b/src/pages/api/reset/sendVerification.ts @@ -4,21 +4,27 @@ import {getAuth as getAdminAuth, UserRecord} from "firebase-admin/auth"; import {app, adminApp} from "@/firebase"; import {sessionOptions} from "@/lib/session"; import {withIronSessionApiRoute} from "iron-session/next"; - -const auth = getAuth(app); +import {prepareMailer, prepareMailOptions} from "@/email"; +import ShortUniqueId from "short-unique-id"; export default withIronSessionApiRoute(sendVerification, sessionOptions); async function sendVerification(req: NextApiRequest, res: NextApiResponse) { + const short = new ShortUniqueId(); + if (req.session.user) { - sendSignInLinkToEmail(auth, req.session.user.email, { - url: `https://platform.encoach.com/${req.session.user.email}`, - handleCodeInApp: true, - }) - .then(() => res.status(200).json({ok: true})) - .catch((e) => { - console.log(e); - res.status(404).json({ok: false}); - }); + const transport = prepareMailer("verification"); + const mailOptions = prepareMailOptions( + { + name: req.session.user.name, + code: short.randomUUID(6), + email: req.session.user.email, + }, + [req.session.user.email], + "EnCoach Verification", + "verification", + ); + + await transport.sendMail(mailOptions); } } diff --git a/src/pages/login.tsx b/src/pages/login.tsx index c06c0ed5..aab46e97 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -72,6 +72,9 @@ export default function Login() { setIsLoading(true); axios .post<{ok: boolean}>("/api/reset/sendVerification", {}) + .then(() => { + toast.success("An e-mail has been sent, please make sure to check your spam folder!"); + }) .catch((e) => { console.log(e); toast.error("Something went wrong, please logout and re-login.", {toastId: "send-verify-error"}); diff --git a/src/pages/register.tsx b/src/pages/register.tsx index c0b2182c..120f4fc5 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -79,6 +79,9 @@ export default function Register({code: queryCode}: {code: string}) { setIsLoading(true); axios .post<{ok: boolean}>("/api/reset/sendVerification", {}) + .then(() => { + toast.success("An e-mail has been sent, please make sure to check your spam folder!"); + }) .catch((e) => { console.log(e); toast.error("Something went wrong, please logout and re-login.", {toastId: "send-verify-error"});