-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.config.ts
53 lines (48 loc) · 1.89 KB
/
email.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import nodemailer from "nodemailer";
import { logError } from "../utils/logger";
import { MailOptions } from "nodemailer/lib/json-transport";
import { signEmailTokenJWT } from "./jwt.config";
import { emailTokenFormat } from "../@types/utils/jwt.config.types";
import { SERVER_SUPPORT_PASSWORD, ORIGIN, SERVER_SUPPORT_EMAIL } from "./env.config";
const transporter = nodemailer.createTransport({
service: "gmail",
port: 465,
secure: true,
auth: {
user: SERVER_SUPPORT_EMAIL,
pass: SERVER_SUPPORT_PASSWORD,
},
});
const verificationEmailContent = (token: string) => {
return {
from: SERVER_SUPPORT_EMAIL,
subject: "Email verification",
html: `Hello, click here to verify your email <a href="${ORIGIN}/special/emailVerification/${token}">
<button>click here</button>
</a>`,
};
};
const passwordResetEmailContent = (token: string) => {
return {
from: SERVER_SUPPORT_EMAIL,
subject: "Set your new password",
html: `Hello, click here to set your new password<a href="${ORIGIN}/special/setNewPassword/${token}">
<button>click here</button>
</a>`,
};
};
const sendEmailHandler = (clientEmail: string, emailContent: MailOptions) => {
transporter.sendMail({ ...emailContent, to: clientEmail }, function (error) {
if (error) {
logError("Nodemailer error: " + error);
}
});
};
export const sendResetPasswordEmailHandler = (clientEmail: string, tokenData: emailTokenFormat) => {
const token = signEmailTokenJWT(tokenData);
sendEmailHandler(clientEmail, passwordResetEmailContent(token));
};
export const sendVerificationEmailHandler = (clientEmail: string, tokenData: emailTokenFormat) => {
const token = signEmailTokenJWT(tokenData);
sendEmailHandler(clientEmail, verificationEmailContent(token));
};