diff --git a/backend/src/app/controllers/user/crud/create.ts b/backend/src/app/controllers/user/crud/create.ts index 93ea276..3f9fbab 100644 --- a/backend/src/app/controllers/user/crud/create.ts +++ b/backend/src/app/controllers/user/crud/create.ts @@ -1,5 +1,6 @@ import EmailValidator from "email-validator"; import { Request, Response } from "express"; +import Validate from "../../../email/templates/Validate"; import Users from "../../../models/User"; import { LOGGER } from "../../../util/Logger"; @@ -36,6 +37,9 @@ export default async (req: Request, res: Response) => { const created = new Users({ first, last, email: fmtEmail }); created.password = created.hashPassword(password); await created.save(); + + await Validate(created); + return res.status(200).json(created); } catch (ex) { LOGGER.error(ex); diff --git a/backend/src/app/controllers/user/crud/update.ts b/backend/src/app/controllers/user/crud/update.ts new file mode 100644 index 0000000..bf730e7 --- /dev/null +++ b/backend/src/app/controllers/user/crud/update.ts @@ -0,0 +1,38 @@ +import { Request, Response } from "express"; +import Users from "../../../models/User"; +import { LOGGER } from "../../../util/Logger"; +import Validate from "../../../email/templates/Validate"; + +export default async (req: Request, res: Response) => { + try { + if (!req.user) { + return res + .status(422) + .json({ error: "Cannot modify user since you are not logged in." }); + } + + // If you're an admin it's assumed you're deleting someone else's account for them + const userIdToUpdate = req.user.admin ? req.body._id : req.user._id; + const user = await Users.findById(userIdToUpdate); + if (!user) { + return res + .status(404) + .json({ error: `User with id ${userIdToUpdate} not found` }); + } + + user.first = req.body.first || user.first; + user.last = req.body.last || user.last; + user.email = req.body.email || user.email; + user.isEmailValidated = false; + await user.save(); + + if (req.body.email) { + await Validate(user); + } + + return res.status(200).json({ "Successfully Deleted": user }); + } catch (ex) { + LOGGER.error(ex); + return res.status(500).json({ error: ex }); + } +}; diff --git a/backend/src/app/controllers/user/login.ts b/backend/src/app/controllers/user/login.ts index f3d0d87..e710d39 100644 --- a/backend/src/app/controllers/user/login.ts +++ b/backend/src/app/controllers/user/login.ts @@ -21,6 +21,13 @@ export default async (req: Request, res: Response) => { .json({ error: "Email address is not registered." }); } + if (!user.isEmailValidated) { + return res.status(403).json({ + error: + "This email address has not yet been validated. Please check your email.", + }); + } + if (!user.validPassword(req.body.password)) { return res .status(404) diff --git a/backend/src/app/controllers/user/routes.ts b/backend/src/app/controllers/user/routes.ts index 96ac593..a9ff349 100644 --- a/backend/src/app/controllers/user/routes.ts +++ b/backend/src/app/controllers/user/routes.ts @@ -2,6 +2,7 @@ import { RouteCollection } from "../routes"; import create from "./crud/create"; import del from "./crud/delete"; import login from "./login"; +import validate from "./validate"; const routes: RouteCollection[] = [ { @@ -19,6 +20,11 @@ const routes: RouteCollection[] = [ path: "/api/v1/user/login", handler: login, }, + { + httpMethod: "GET", + path: "/api/v1/user/validateEmail", + handler: validate, + }, ]; export default routes; diff --git a/backend/src/app/controllers/user/validate.ts b/backend/src/app/controllers/user/validate.ts new file mode 100644 index 0000000..d6a7213 --- /dev/null +++ b/backend/src/app/controllers/user/validate.ts @@ -0,0 +1,23 @@ +import { Request, Response } from "express"; +import Users from "../../models/User"; +import { LOGGER } from "../../util/Logger"; + +export default async (req: Request, res: Response) => { + try { + const user = await Users.findById(req.query._id); + if (!user) { + return res.status(404).send("User not found"); + } + + user.isEmailValidated = true; + await user.save(); + return res + .status(200) + .send( + "Thank you for validating your email address! You may now close this window." + ); + } catch (ex) { + LOGGER.error(ex); + return res.status(500).send("Uh oh! Something went wrong.."); + } +}; diff --git a/backend/src/app/email/Email.ts b/backend/src/app/email/Email.ts new file mode 100644 index 0000000..df68c6d --- /dev/null +++ b/backend/src/app/email/Email.ts @@ -0,0 +1,15 @@ +import NodeMailer from "nodemailer"; + +export const createTransport = () => { + return NodeMailer.createTransport({ + host: "smtp.titan.email", + port: 465, + auth: { + user: process.env.NODEMAILER_ADDRESS, + pass: process.env.NODEMAILER_PASSWORD, + }, + tls: { secureProtocol: "TLSv1_2_method", rejectUnauthorized: false }, + ignoreTLS: false, + requireTLS: true, + }); +}; diff --git a/backend/src/app/email/templates/Validate.ts b/backend/src/app/email/templates/Validate.ts new file mode 100644 index 0000000..8262ec5 --- /dev/null +++ b/backend/src/app/email/templates/Validate.ts @@ -0,0 +1,90 @@ +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/middleware/UserInjection.ts b/backend/src/app/middleware/UserInjection.ts index 4dd1c28..165d746 100644 --- a/backend/src/app/middleware/UserInjection.ts +++ b/backend/src/app/middleware/UserInjection.ts @@ -2,6 +2,7 @@ import { NextFunction, Request, Response } from "express"; import * as jwt from "jsonwebtoken"; import moment from "moment"; import Users from "../models/User"; +import { LOGGER } from "../util/Logger"; export default () => { return async (req: Request, res: Response, next: NextFunction) => { @@ -21,10 +22,11 @@ export default () => { } const user = await Users.findById(_id); - req.user = user; - next(); + req.user = user.isEmailValidated ? user : null; + return next(); } catch (err) { - return res.status(400).json({ error: `Invalid Token: ${err}` }); + LOGGER.error(err); + return next(); } }; }; diff --git a/backend/src/app/models/User.ts b/backend/src/app/models/User.ts index af7aad4..d6e7568 100644 --- a/backend/src/app/models/User.ts +++ b/backend/src/app/models/User.ts @@ -7,6 +7,7 @@ export type UserModel = Document & { email: string; password: string; admin: boolean; + isEmailValidated: boolean; hashPassword: (password: string) => string; validPassword: (password: string) => boolean; }; @@ -16,6 +17,10 @@ const UserSchema = new Schema({ last: String, email: String, password: String, + isEmailValidated: { + type: Boolean, + default: false, + }, admin: { type: Boolean, default: false,