From 0d5cd1f4a57709e47deaca5360101e027d31af36 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Fri, 15 Apr 2022 07:58:47 -0500 Subject: [PATCH] Cleanup, add reset password func --- backend/src/app/controllers/admin/routes.ts | 1 - backend/src/app/controllers/routes.ts | 2 - .../src/app/controllers/user/crud/create.ts | 4 +- .../src/app/controllers/user/crud/forgot.ts | 25 ++++++ .../src/app/controllers/user/crud/update.ts | 4 +- .../app/email/templates/ResetPasswordEmail.ts | 30 +++++++ backend/src/app/email/templates/Validate.ts | 90 ------------------- .../src/app/email/templates/ValidateEmail.ts | 19 ++++ 8 files changed, 78 insertions(+), 97 deletions(-) delete mode 100644 backend/src/app/controllers/admin/routes.ts create mode 100644 backend/src/app/controllers/user/crud/forgot.ts create mode 100644 backend/src/app/email/templates/ResetPasswordEmail.ts delete mode 100644 backend/src/app/email/templates/Validate.ts create mode 100644 backend/src/app/email/templates/ValidateEmail.ts diff --git a/backend/src/app/controllers/admin/routes.ts b/backend/src/app/controllers/admin/routes.ts deleted file mode 100644 index d6d1738..0000000 --- a/backend/src/app/controllers/admin/routes.ts +++ /dev/null @@ -1 +0,0 @@ -export default []; diff --git a/backend/src/app/controllers/routes.ts b/backend/src/app/controllers/routes.ts index 1a8ef45..7a7b850 100644 --- a/backend/src/app/controllers/routes.ts +++ b/backend/src/app/controllers/routes.ts @@ -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; diff --git a/backend/src/app/controllers/user/crud/create.ts b/backend/src/app/controllers/user/crud/create.ts index 3f9fbab..e040166 100644 --- a/backend/src/app/controllers/user/crud/create.ts +++ b/backend/src/app/controllers/user/crud/create.ts @@ -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) { diff --git a/backend/src/app/controllers/user/crud/forgot.ts b/backend/src/app/controllers/user/crud/forgot.ts new file mode 100644 index 0000000..1831261 --- /dev/null +++ b/backend/src/app/controllers/user/crud/forgot.ts @@ -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 }); + } +}; diff --git a/backend/src/app/controllers/user/crud/update.ts b/backend/src/app/controllers/user/crud/update.ts index bf730e7..ba1beff 100644 --- a/backend/src/app/controllers/user/crud/update.ts +++ b/backend/src/app/controllers/user/crud/update.ts @@ -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 }); diff --git a/backend/src/app/email/templates/ResetPasswordEmail.ts b/backend/src/app/email/templates/ResetPasswordEmail.ts new file mode 100644 index 0000000..b75067c --- /dev/null +++ b/backend/src/app/email/templates/ResetPasswordEmail.ts @@ -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: ` Your Password Reset Request for ${process.env.APP_NAME}
 
logo
 
 

You have requested to reset your password

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.

If you did not request a password reset, please disregard this email entirely.

Reset Password
 
 

© ${process.env.URL}

 
`, + }; + + try { + await transporter.sendMail(mailOpts); + } catch (ex) { + LOGGER.error(ex); + } +}; diff --git a/backend/src/app/email/templates/Validate.ts b/backend/src/app/email/templates/Validate.ts deleted file mode 100644 index 8262ec5..0000000 --- a/backend/src/app/email/templates/Validate.ts +++ /dev/null @@ -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: ` - - - - - - Validate Your Email Address for ${process.env.APP_NAME} - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
 
logo
 
- - - - - - - - - - -
 
-

Please Validate your Email

-

A new account for ${process.env.APP_NAME} has been registered to this email address.

-

To verify this email, press the button below. If you did not register with this email address, you can ignore this email entirely.

- Reset Password -
 
-
 
-

© - http://${process.env.URL}/

-
 
-
- - - - `, - }; - - try { - await transporter.sendMail(mailOpts); - } catch (ex) { - LOGGER.error(ex); - } -}; diff --git a/backend/src/app/email/templates/ValidateEmail.ts b/backend/src/app/email/templates/ValidateEmail.ts new file mode 100644 index 0000000..c3c2771 --- /dev/null +++ b/backend/src/app/email/templates/ValidateEmail.ts @@ -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: ` Validate Your Email Address for ${process.env.APP_NAME}
 
logo
 
 

Please Validate your Email

A new account for ${process.env.APP_NAME}has been registered to this email address.

To verify this email, press the button below. If you did not register with this email address, you can ignore this email entirely.

Reset Password
 
 

© http://${process.env.URL}/

 
`, + }; + + try { + await transporter.sendMail(mailOpts); + } catch (ex) { + LOGGER.error(ex); + } +};