Skip to content

Commit

Permalink
Merge pull request #2 from p-society/dev
Browse files Browse the repository at this point in the history
feat: templating
  • Loading branch information
zakhaev26 authored Aug 8, 2024
2 parents 46df97d + 70881f9 commit 4ed8a80
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/services/forgot-password/forgot-password.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { BadRequest } from "@feathersjs/errors";
import { Application } from "@feathersjs/express";
import { Request, Response } from "express";
import Mailer from "../../mailer";
import forgotPassword from "../../templates/forgot-password/index";

const EXPIRATION_OFFSET: number = 10; // 10 Minutes
export default function (app: Application): void {
app.use("/forgot-password", async function (req: Request, res: Response) {
try {
const { email } = req.body;
/**
* @zakhaev26
*@todo improve locales by adding more local languages if needed.
*/
// const { lang } = req.query;

if (!email) throw new BadRequest("Email not provided");

const user = await app.service("users")._find({
query: {
email: email,
$limit: 1,
},
paginate: false,
});

if (user.length === 0) throw new Error("No user found");
// user exists,so create OTP

const today = new Date();
today.setMinutes(today.getMinutes() + EXPIRATION_OFFSET);
const expirationDate = new Date(today);

const OTP = generateOTP();

await app.service("reset-password")._create({
user: user[0]._id,
otp: OTP,
email: email,
expiresAt: expirationDate,
});

const tmpl: string = forgotPassword["en"].render({
username: user[0].username,
otp: OTP,
expiration: EXPIRATION_OFFSET, //template has minutes
});
await new Mailer().sendPasswordResetOTP(
[email],
"Reset Your Password",
tmpl
);
res.json({
message: "OTP sent successfully on E-mail Id",
});
} catch (error: any) {
console.error(error);
throw new Error(error);
}
});
}

function generateOTP() {
const digits = "0123456789";
let OTP = "";

for (let i = 0; i < 4; i++) {
const randomIndex = Math.floor(Math.random() * digits.length);
OTP += digits[randomIndex];
}

return OTP;
}
74 changes: 74 additions & 0 deletions src/templates/forgot-password/en/forgot-password.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Reset Your Password on IIIT-Room Master</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
padding-bottom: 20px;
}
.header img {
max-width: 100px;
}
.content {
text-align: center;
}
.otp {
font-size: 24px;
font-weight: bold;
margin: 20px 0;
}
.footer {
text-align: center;
color: #888;
font-size: 12px;
margin-top: 20px;
}
</style>
</head>

<body>
<div class="container">
<div class="header">
<h1>Reset Your Password</h1>
</div>
<div class="content">
<p>Hi {{firstName }},</p>
<p>We received a request to reset your password for your Room Master account. Please use the following OTP to reset your
password:</p>
<div class="otp">{{otp}}</div>
<p>This OTP is valid for the next {{ expiration }} minutes. Please do not share this OTP with anyone.</p>
<p>If you did not request a password reset, please ignore this email or contact our support team for assistance.
</p>
</div>
<div class="footer">
<p>Thanks,<br>Room Master Team</p>
<p>Note: This is an automated email. Please do not reply to this email.</p>
</div>
</div>
</body>

</html>
95 changes: 95 additions & 0 deletions src/templates/forgot-password/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import Twig from "twig";
// import fs from 'fs';

// require.extensions['.twig'] = function (module, filename) {
// module.exports = fs.readFileSync(filename, 'utf8');
// };

// // @ts-ignore
// import en from './en/forgot-password.html.twig';
// // @ts-ignore
// import fr from './fr/forgot-password.html.twig';

// @temporarily keeping the twigfiles here due to build issues,
// @todo: find a way to get the templates dir in build

const en = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Reset Your Password on IIIT-Room Master</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
color: #333;
margin: 0;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
padding-bottom: 20px;
}
.header img {
max-width: 100px;
}
.content {
text-align: center;
}
.otp {
font-size: 24px;
font-weight: bold;
margin: 20px 0;
}
.footer {
text-align: center;
color: #888;
font-size: 12px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Reset Your Password</h1>
</div>
<div class="content">
<p>Hi {{firstName }},</p>
<p>We received a request to reset your password for your Room Master account. Please use the following OTP to reset your
password:</p>
<div class="otp">{{otp}}</div>
<p>This OTP is valid for the next {{ expiration }} minutes. Please do not share this OTP with anyone.</p>
<p>If you did not request a password reset, please ignore this email or contact our support team for assistance.
</p>
</div>
<div class="footer">
<p>Thanks,<br>Room Master Team</p>
<p>Note: This is an automated email. Please do not reply to this email.</p>
</div>
</div>
</body>
</html>`;

export default {
en: Twig.twig({
data: en,
}),
};
Empty file.

0 comments on commit 4ed8a80

Please sign in to comment.