Components for user modification, email validation
This commit is contained in:
parent
01227bb28f
commit
99a3d7ff33
9 changed files with 193 additions and 3 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import EmailValidator from "email-validator";
|
import EmailValidator from "email-validator";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
|
import Validate from "../../../email/templates/Validate";
|
||||||
import Users from "../../../models/User";
|
import Users from "../../../models/User";
|
||||||
import { LOGGER } from "../../../util/Logger";
|
import { LOGGER } from "../../../util/Logger";
|
||||||
|
|
||||||
|
|
@ -36,6 +37,9 @@ export default async (req: Request, res: Response) => {
|
||||||
const created = new Users({ first, last, email: fmtEmail });
|
const created = new Users({ first, last, email: fmtEmail });
|
||||||
created.password = created.hashPassword(password);
|
created.password = created.hashPassword(password);
|
||||||
await created.save();
|
await created.save();
|
||||||
|
|
||||||
|
await Validate(created);
|
||||||
|
|
||||||
return res.status(200).json(created);
|
return res.status(200).json(created);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
LOGGER.error(ex);
|
LOGGER.error(ex);
|
||||||
|
|
|
||||||
38
backend/src/app/controllers/user/crud/update.ts
Normal file
38
backend/src/app/controllers/user/crud/update.ts
Normal file
|
|
@ -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 });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -21,6 +21,13 @@ export default async (req: Request, res: Response) => {
|
||||||
.json({ error: "Email address is not registered." });
|
.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)) {
|
if (!user.validPassword(req.body.password)) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { RouteCollection } from "../routes";
|
||||||
import create from "./crud/create";
|
import create from "./crud/create";
|
||||||
import del from "./crud/delete";
|
import del from "./crud/delete";
|
||||||
import login from "./login";
|
import login from "./login";
|
||||||
|
import validate from "./validate";
|
||||||
|
|
||||||
const routes: RouteCollection[] = [
|
const routes: RouteCollection[] = [
|
||||||
{
|
{
|
||||||
|
|
@ -19,6 +20,11 @@ const routes: RouteCollection[] = [
|
||||||
path: "/api/v1/user/login",
|
path: "/api/v1/user/login",
|
||||||
handler: login,
|
handler: login,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
httpMethod: "GET",
|
||||||
|
path: "/api/v1/user/validateEmail",
|
||||||
|
handler: validate,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export default routes;
|
export default routes;
|
||||||
|
|
|
||||||
23
backend/src/app/controllers/user/validate.ts
Normal file
23
backend/src/app/controllers/user/validate.ts
Normal file
|
|
@ -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..");
|
||||||
|
}
|
||||||
|
};
|
||||||
15
backend/src/app/email/Email.ts
Normal file
15
backend/src/app/email/Email.ts
Normal file
|
|
@ -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,
|
||||||
|
});
|
||||||
|
};
|
||||||
90
backend/src/app/email/templates/Validate.ts
Normal file
90
backend/src/app/email/templates/Validate.ts
Normal file
|
|
@ -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: `
|
||||||
|
<!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;"> </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;"> </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;"> </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;"> </td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
<tr>
|
||||||
|
<td style="height:20px;"> </td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="text-align:center;">
|
||||||
|
<p style="font-size:14px; color:#076778; line-height:18px; margin:0 0 0;">©
|
||||||
|
<strong>http://${process.env.URL}/</strong></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td style="height:80px;"> </td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
`,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
await transporter.sendMail(mailOpts);
|
||||||
|
} catch (ex) {
|
||||||
|
LOGGER.error(ex);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -2,6 +2,7 @@ import { NextFunction, Request, Response } from "express";
|
||||||
import * as jwt from "jsonwebtoken";
|
import * as jwt from "jsonwebtoken";
|
||||||
import moment from "moment";
|
import moment from "moment";
|
||||||
import Users from "../models/User";
|
import Users from "../models/User";
|
||||||
|
import { LOGGER } from "../util/Logger";
|
||||||
|
|
||||||
export default () => {
|
export default () => {
|
||||||
return async (req: Request, res: Response, next: NextFunction) => {
|
return async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
|
@ -21,10 +22,11 @@ export default () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await Users.findById(_id);
|
const user = await Users.findById(_id);
|
||||||
req.user = user;
|
req.user = user.isEmailValidated ? user : null;
|
||||||
next();
|
return next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return res.status(400).json({ error: `Invalid Token: ${err}` });
|
LOGGER.error(err);
|
||||||
|
return next();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ export type UserModel = Document & {
|
||||||
email: string;
|
email: string;
|
||||||
password: string;
|
password: string;
|
||||||
admin: boolean;
|
admin: boolean;
|
||||||
|
isEmailValidated: boolean;
|
||||||
hashPassword: (password: string) => string;
|
hashPassword: (password: string) => string;
|
||||||
validPassword: (password: string) => boolean;
|
validPassword: (password: string) => boolean;
|
||||||
};
|
};
|
||||||
|
|
@ -16,6 +17,10 @@ const UserSchema = new Schema<UserModel>({
|
||||||
last: String,
|
last: String,
|
||||||
email: String,
|
email: String,
|
||||||
password: String,
|
password: String,
|
||||||
|
isEmailValidated: {
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
},
|
||||||
admin: {
|
admin: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false,
|
default: false,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue