Replaced the previous e-mail verification process for our own
This commit is contained in:
@@ -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"),
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<div style="background-color: #ffffff; color: #353338;"
|
||||
class="h-full min-h-screen w-full flex flex-col p-8 gap-16 text-base" class="text-base">
|
||||
class="h-full min-h-screen w-full flex flex-col p-8 gap-16 text-base">
|
||||
<img src="/logo_title.png" class="w-48 h-48 self-center" />
|
||||
<div>
|
||||
<span>Hello future {{type}} of <b>EnCoach</b>,</span><br />
|
||||
|
||||
22
src/email/templates/verification.handlebars
Normal file
22
src/email/templates/verification.handlebars
Normal file
@@ -0,0 +1,22 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
|
||||
</head>
|
||||
<div>
|
||||
<p>Hello {{name}},</p>
|
||||
<br />
|
||||
<p>Follow this link to verify your email address.</p>
|
||||
<a href="https://platform.encoach.com/action?mode=signIn&continueUrl={{email}}&oobCode={{code}}">Verify account</a>
|
||||
<br />
|
||||
<br />
|
||||
<p>If you didn’t ask to verify this address, you can ignore this email.</p>
|
||||
<br />
|
||||
<p>Thanks,</p>
|
||||
<p>Your EnCoach team</p>
|
||||
</div>
|
||||
|
||||
</html>
|
||||
5
src/email/templates/verification.handlebars.json
Normal file
5
src/email/templates/verification.handlebars.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "Tiago Ribeiro",
|
||||
"email": "tiago.ribeiro@ecrop.dev",
|
||||
"code": "123"
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"});
|
||||
|
||||
@@ -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"});
|
||||
|
||||
Reference in New Issue
Block a user