Skip to content

Commit 4369820

Browse files
committed
Add email template and add email config file
1 parent fe8c440 commit 4369820

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import dotenv from "dotenv";
2+
3+
dotenv.config();
4+
5+
export const emailOptions = {
6+
user: process.env.SMTP_USER,
7+
password: process.env.SMTP_PASSWORD,
8+
smtp_host: process.env.SMTP_HOST,
9+
smtp_port: Number(process.env.SMTP_PORT),
10+
};

backend/user-service/controller/user-controller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
import { BadRequestError, ConflictError, NotFoundError, UnauthorisedError } from "../utils/httpErrors.js";
1717
import TokenService from "../services/tokenService.js";
1818
import { sendEmail } from "../services/emailService.js";
19+
import dotenv from "dotenv";
1920

21+
dotenv.config();
2022
const PASSWORD_SALT = 10;
2123
export async function createUser(req, res, next) {
2224
try {
@@ -167,7 +169,7 @@ export async function forgetPassword(req, res, next) {
167169
try {
168170
const { email } = req.body;
169171
const resetToken = await TokenService.generateResetToken(email);
170-
const resetPasswordLink = `http://localhost:3000/reset-password?token=${resetToken}`;
172+
const resetPasswordLink = `${process.env.APP_URL}/reset-password?token=${resetToken}`;
171173
await sendEmail({
172174
to: email,
173175
subject: "Reset password",

backend/user-service/services/emailService.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import nodemailer from "nodemailer";
2-
import dotenv from "dotenv";
3-
4-
dotenv.config();
2+
import { emailOptions } from "../config/emailConfig";
53

64
const transporter = nodemailer.createTransport({
7-
host: process.env.SMTP_HOST,
8-
port: Number(process.env.SMTP_PORT),
5+
host: emailOptions.smtp_host,
6+
port: emailOptions.smtp_port,
97
auth: {
10-
user: process.env.SMTP_USER,
11-
pass: process.env.SMTP_PASSWORD,
8+
user: emailOptions.user,
9+
pass: emailOptions.password,
1210
},
1311
});
1412

@@ -21,9 +19,9 @@ transporter.verify(function (error, success) {
2119
});
2220

2321
export const sendEmail = async ({ to, subject, text, html }) => {
24-
const emailOptions = { from: process.env.SMTP_USER, to, subject, text, html };
22+
const emailOptions = { from: emailOptions.user, to, subject, text, html };
2523
try {
26-
const res = await transporter.sendMail(emailOptions);
24+
await transporter.sendMail(emailOptions);
2725
} catch (err) {
2826
console.error(err);
2927
throw err;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const getResetPasswordEmailTemplate = (passwordResetLink) => `
2+
3+
<p>Dear user,</p>
4+
<p>You have requested to change your password.</p>
5+
<p>You can click <a href=${passwordResetLink}>here</a>, which will bring you to a password reset page to
6+
reset your password. The link is only valid for <b>15 minutes</b></p>
7+
`;

0 commit comments

Comments
 (0)