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 { 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);
|
||||
|
|
|
|||
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 });
|
||||
}
|
||||
};
|
||||
Loading…
Reference in a new issue