Cleanup, add reset password func

This commit is contained in:
oitsjustjose 2022-04-15 07:58:47 -05:00
commit 0d5cd1f4a5
8 changed files with 78 additions and 97 deletions

View file

@ -1 +0,0 @@
export default [];

View file

@ -1,5 +1,4 @@
import { Request, Response } from "express";
import AdminRoutes from "./admin/routes";
import ProductRoutes from "./product/routes";
import ReviewRoutes from "./review/routes";
import UserRoutes from "./user/routes";
@ -14,7 +13,6 @@ const routes: RouteCollection[] = [
...ProductRoutes,
...UserRoutes,
...ReviewRoutes,
...AdminRoutes,
];
export default routes;

View file

@ -1,6 +1,6 @@
import EmailValidator from "email-validator";
import { Request, Response } from "express";
import Validate from "../../../email/templates/Validate";
import ValidateEmail from "../../../email/templates/ValidateEmail";
import Users from "../../../models/User";
import { LOGGER } from "../../../util/Logger";
@ -38,7 +38,7 @@ export default async (req: Request, res: Response) => {
created.password = created.hashPassword(password);
await created.save();
await Validate(created);
await ValidateEmail(created);
return res.status(200).json(created);
} catch (ex) {

View file

@ -0,0 +1,25 @@
import { Request, Response } from "express";
import Users from "../../../models/User";
import ResetPasswordEmail from "../../../email/templates/ResetPasswordEmail";
export default async (req: Request, res: Response) => {
try {
const email: string | null = req.query.email as string;
if (!email) {
return res.status(422).json({ error: "No email provided." });
}
const user = await Users.findOne({ email });
if (!user) {
return res
.status(404)
.json({ error: "User with given email not found." });
}
await ResetPasswordEmail(user);
return res.status(200).send();
} catch (ex) {
return res.status(500).json({ error: ex });
}
};

View file

@ -1,7 +1,7 @@
import { Request, Response } from "express";
import Users from "../../../models/User";
import { LOGGER } from "../../../util/Logger";
import Validate from "../../../email/templates/Validate";
import ValidateEmail from "../../../email/templates/ValidateEmail";
export default async (req: Request, res: Response) => {
try {
@ -27,7 +27,7 @@ export default async (req: Request, res: Response) => {
await user.save();
if (req.body.email) {
await Validate(user);
await ValidateEmail(user);
}
return res.status(200).json({ "Successfully Deleted": user });

View file

@ -0,0 +1,30 @@
import * as jwt from "jsonwebtoken";
import moment from "moment";
import { UserModel } from "../../models/User";
import { LOGGER } from "../../util/Logger";
import { createTransport } from "../Email";
export default async (user: UserModel) => {
const token = jwt.sign(
{
_id: user._id,
email: user.email,
expiresAt: moment().add(10, "minutes").toISOString(),
},
process.env.JWT_SECRET as string
);
const transporter = createTransport();
const mailOpts = {
to: user.email,
from: process.env.NODEMAILER_ADDRESS,
subject: `Your Password Reset Request for ${process.env.APP_NAME}`,
html: `<!DOCTYPE html> <html lang="en-US"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> <title>Your Password Reset Request for ${process.env.APP_NAME}</title> <style type="text/css"> a:hover{text-decoration: underline !important;}</style> </head> <body marginheight="0" topmargin="0" marginwidth="0" style="margin: 0px; background-color: #f2f3f8;" leftmargin="0"> <table cellspacing="0" border="0" cellpadding="0" width="100%" bgcolor="#f2f3f8" style="@import url(https://fonts.googleapis.com/css?family=Rubik:300,400,500,700|Open+Sans:300,400,600,700); font-family: 'Open Sans', sans-serif;"> <tr> <td> <table style="background-color: #f2f3f8; max-width:670px; margin:0 auto;" width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td style="height:80px;">&nbsp;</td></tr><tr> <td style="text-align:center;"> <a href="http://${process.env.URL}" title="logo" target="_blank"> <img width="60" src="${process.env.LOGO}" title="logo" alt="logo"> </a> </td></tr><tr> <td style="height:20px;">&nbsp;</td></tr><tr> <td> <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" style="max-width:670px;background:#fff; border-radius:3px; text-align:center;-webkit-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);-moz-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);box-shadow:0 6px 18px 0 rgba(0,0,0,.06);"> <tr> <td style="height:40px;">&nbsp;</td></tr><tr> <td style="padding:0 35px;"> <h1 style="color:#1e1e2d; font-weight:500; margin:0;font-size:32px;font-family:'Rubik',sans-serif;"> You have requested to reset your password </h1> <span style="display:inline-block; vertical-align:middle; margin:29px 0 26px; border-bottom:1px solid #cecece; width:100px;"></span> <p style="color:#455056; font-size:15px;line-height:24px; margin:0;"> A unique link to reset your password has been generated for you. To reset your password, click the following link and follow the instructions. This link will expire in 10 minutes. </p><p> <strong> If you did not request a password reset, please disregard this email entirely. </strong> </p><a href="http://${process.env.URL}/ResetPassword?t=${token}" style="background:#00bee0;text-decoration:none !important; font-weight:500; margin-top:35px; color:#fff;text-transform:uppercase; font-size:14px;padding:10px 24px;display:inline-block;border-radius:50px;"> Reset Password </a> </td></tr><tr> <td style="height:40px;">&nbsp;</td></tr></table> </td><tr> <td style="height:20px;">&nbsp;</td></tr><tr> <td style="text-align:center;"> <p style="font-size:14px; color:#076778; line-height:18px; margin:0 0 0;">&copy; <strong>${process.env.URL}</strong></p></td></tr><tr> <td style="height:80px;">&nbsp;</td></tr></table> </td></tr></table> </body> </html>`,
};
try {
await transporter.sendMail(mailOpts);
} catch (ex) {
LOGGER.error(ex);
}
};

View file

@ -1,90 +0,0 @@
import { UserModel } from "../../models/User";
import { LOGGER } from "../../util/Logger";
import { createTransport } from "../Email";
export default async (user: UserModel) => {
const transporter = createTransport();
const mailOpts = {
to: user.email,
from: process.env.EMAIL_ADDR,
subject: `Please Validate your Email Address for ${process.env.APP_NAME}`,
html: `
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Validate Your Email Address for ${process.env.APP_NAME}</title>
<style type="text/css">a:hover {text-decoration: underline !important;}</style>
</head>
<body marginheight="0" topmargin="0" marginwidth="0" style="margin: 0px; background-color: #f2f3f8;" leftmargin="0">
<table cellspacing="0" border="0" cellpadding="0" width="100%" bgcolor="#f2f3f8"
style="@import url(https://fonts.googleapis.com/css?family=Rubik:300,400,500,700|Open+Sans:300,400,600,700); font-family: 'Open Sans', sans-serif;">
<tr>
<td>
<table style="background-color: #f2f3f8; max-width:670px; margin:0 auto;" width="100%" border="0"
align="center" cellpadding="0" cellspacing="0">
<tr>
<td style="height:80px;">&nbsp;</td>
</tr>
<tr>
<td style="text-align:center;"> <a href="http://${process.env.URL}" title="logo"
target="_blank"> <img width="60" src="${process.env.LOGO}" title="logo"
alt="logo"> </a> </td>
</tr>
<tr>
<td style="height:20px;">&nbsp;</td>
</tr>
<tr>
<td>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0"
style="max-width:670px;background:#fff; border-radius:3px; text-align:center;-webkit-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);-moz-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);box-shadow:0 6px 18px 0 rgba(0,0,0,.06);">
<tr>
<td style="height:40px;">&nbsp;</td>
</tr>
<tr>
<td style="padding:0 35px;">
<h1
style="color:#1e1e2d; font-weight:500; margin:0;font-size:32px;font-family:'Rubik',sans-serif;">Please Validate your Email</h1> <span
style="display:inline-block; vertical-align:middle; margin:29px 0 26px; border-bottom:1px solid #cecece; width:100px;"></span>
<p style="color:#455056; font-size:15px;line-height:24px; margin:0;">A new account for ${process.env.APP_NAME} has been registered to this email address.</p>
<p> <strong> To verify this email, press the button below. If you did not register with this email address, you can ignore this email entirely.</strong> </p><a
href="http://${process.env.URL}/api/v1/user/validateEmail?_id=${user._id}"
style="background:#00bee0;text-decoration:none !important; font-weight:500; margin-top:35px; color:#fff;text-transform:uppercase; font-size:14px;padding:10px 24px;display:inline-block;border-radius:50px;">
Reset Password </a>
</td>
</tr>
<tr>
<td style="height:40px;">&nbsp;</td>
</tr>
</table>
</td>
<tr>
<td style="height:20px;">&nbsp;</td>
</tr>
<tr>
<td style="text-align:center;">
<p style="font-size:14px; color:#076778; line-height:18px; margin:0 0 0;">&copy;
<strong>http://${process.env.URL}/</strong></p>
</td>
</tr>
<tr>
<td style="height:80px;">&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
`,
};
try {
await transporter.sendMail(mailOpts);
} catch (ex) {
LOGGER.error(ex);
}
};

View file

@ -0,0 +1,19 @@
import { UserModel } from "../../models/User";
import { LOGGER } from "../../util/Logger";
import { createTransport } from "../Email";
export default async (user: UserModel) => {
const transporter = createTransport();
const mailOpts = {
to: user.email,
from: process.env.NODEMAILER_ADDRESS,
subject: `Please Validate your Email Address for ${process.env.APP_NAME}`,
html: `<!DOCTYPE html> <html lang="en-US"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/> <title>Validate Your Email Address for ${process.env.APP_NAME}</title> <style type="text/css">a:hover{text-decoration: underline !important;}</style> </head> <body marginheight="0" topmargin="0" marginwidth="0" style="margin: 0px; background-color: #f2f3f8;" leftmargin="0"> <table cellspacing="0" border="0" cellpadding="0" width="100%" bgcolor="#f2f3f8" style="@import url(https://fonts.googleapis.com/css?family=Rubik:300,400,500,700|Open+Sans:300,400,600,700); font-family: 'Open Sans', sans-serif;"> <tr> <td> <table style="background-color: #f2f3f8; max-width:670px; margin:0 auto;" width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td style="height:80px;">&nbsp;</td></tr><tr> <td style="text-align:center;"> <a href="http://${process.env.URL}" title="logo" target="_blank"> <img width="60" src="${process.env.LOGO}" title="logo" alt="logo"> </a> </td></tr><tr> <td style="height:20px;">&nbsp;</td></tr><tr> <td> <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0" style="max-width:670px;background:#fff; border-radius:3px; text-align:center;-webkit-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);-moz-box-shadow:0 6px 18px 0 rgba(0,0,0,.06);box-shadow:0 6px 18px 0 rgba(0,0,0,.06);"> <tr> <td style="height:40px;">&nbsp;</td></tr><tr> <td style="padding:0 35px;"> <h1 style="color:#1e1e2d; font-weight:500; margin:0;font-size:32px;font-family:'Rubik',sans-serif;">Please Validate your Email</h1> <span style="display:inline-block; vertical-align:middle; margin:29px 0 26px; border-bottom:1px solid #cecece; width:100px;"></span> <p style="color:#455056; font-size:15px;line-height:24px; margin:0;">A new account for ${process.env.APP_NAME}has been registered to this email address.</p><p> <strong> To verify this email, press the button below. If you did not register with this email address, you can ignore this email entirely.</strong> </p><a href="http://${process.env.URL}/api/v1/user/validateEmail?_id=${user._id}" style="background:#00bee0;text-decoration:none !important; font-weight:500; margin-top:35px; color:#fff;text-transform:uppercase; font-size:14px;padding:10px 24px;display:inline-block;border-radius:50px;"> Reset Password </a> </td></tr><tr> <td style="height:40px;">&nbsp;</td></tr></table> </td><tr> <td style="height:20px;">&nbsp;</td></tr><tr> <td style="text-align:center;"> <p style="font-size:14px; color:#076778; line-height:18px; margin:0 0 0;">&copy; <strong>http://${process.env.URL}/</strong></p></td></tr><tr> <td style="height:80px;">&nbsp;</td></tr></table> </td></tr></table> </body> </html>`,
};
try {
await transporter.sendMail(mailOpts);
} catch (ex) {
LOGGER.error(ex);
}
};