Significant API progress
This commit is contained in:
parent
ea72d194c2
commit
01227bb28f
27 changed files with 2378 additions and 0 deletions
44
backend/src/app/controllers/user/crud/create.ts
Normal file
44
backend/src/app/controllers/user/crud/create.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import EmailValidator from "email-validator";
|
||||
import { Request, Response } from "express";
|
||||
import Users from "../../../models/User";
|
||||
import { LOGGER } from "../../../util/Logger";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { first, last, email, password } = req.body;
|
||||
const fmtEmail = email.toLowerCase();
|
||||
|
||||
if (!first || !last || !first.length || !last.length) {
|
||||
return res.status(422).json({
|
||||
error: "First and Last Name are Required Fields",
|
||||
});
|
||||
}
|
||||
|
||||
if (!fmtEmail || !fmtEmail.length || !EmailValidator.validate(fmtEmail)) {
|
||||
return res.status(422).json({
|
||||
error: "A valid email is required",
|
||||
});
|
||||
}
|
||||
|
||||
if (!password || password.length < 8) {
|
||||
return res.status(422).json({
|
||||
error: "Password is required and must contain at least 8 characters",
|
||||
});
|
||||
}
|
||||
|
||||
const existing = await Users.find({ email: fmtEmail });
|
||||
if (existing && existing.length) {
|
||||
return res
|
||||
.status(422)
|
||||
.json({ error: `Email address ${fmtEmail} is already registered.` });
|
||||
}
|
||||
|
||||
const created = new Users({ first, last, email: fmtEmail });
|
||||
created.password = created.hashPassword(password);
|
||||
await created.save();
|
||||
return res.status(200).json(created);
|
||||
} catch (ex) {
|
||||
LOGGER.error(ex);
|
||||
return res.status(500).json({ error: ex });
|
||||
}
|
||||
};
|
||||
29
backend/src/app/controllers/user/crud/delete.ts
Normal file
29
backend/src/app/controllers/user/crud/delete.ts
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import EmailValidator from "email-validator";
|
||||
import { Request, Response } from "express";
|
||||
import Users from "../../../models/User";
|
||||
import { LOGGER } from "../../../util/Logger";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.user) {
|
||||
return res
|
||||
.status(422)
|
||||
.json({ error: "Cannot delete 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 userIdToDel = req.user.admin ? req.body._id : req.user._id;
|
||||
const user = await Users.findById(userIdToDel);
|
||||
if (!user) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: `User with id ${userIdToDel} not found` });
|
||||
}
|
||||
|
||||
await user.delete();
|
||||
return res.status(200).json({ "Successfully Deleted": user });
|
||||
} catch (ex) {
|
||||
LOGGER.error(ex);
|
||||
return res.status(500).json({ error: ex });
|
||||
}
|
||||
};
|
||||
47
backend/src/app/controllers/user/login.ts
Normal file
47
backend/src/app/controllers/user/login.ts
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* @author Jose Stovall: stovallj1995@gmail.com
|
||||
* This file is the REST API endpoint in charge of logging in a user
|
||||
*/
|
||||
|
||||
import { Request, Response } from "express";
|
||||
import * as jwt from "jsonwebtoken";
|
||||
import moment from "moment";
|
||||
import Users, { UserModel } from "../../models/User";
|
||||
import { LOGGER } from "../../util/Logger";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
const user: UserModel | null = await Users.findOne({
|
||||
email: req.body.email.toLowerCase(),
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "Email address is not registered." });
|
||||
}
|
||||
|
||||
if (!user.validPassword(req.body.password)) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "Invalid password for the email address provided." });
|
||||
}
|
||||
|
||||
const token = jwt.sign(
|
||||
{
|
||||
_id: user._id,
|
||||
email: user.email,
|
||||
first: user.first,
|
||||
last: user.last,
|
||||
admin: user.admin,
|
||||
expiresAt: moment().add(1, "day").toISOString(),
|
||||
},
|
||||
process.env.JWT_SECRET as string
|
||||
);
|
||||
|
||||
return res.status(200).json({ token });
|
||||
} catch (ex) {
|
||||
LOGGER.error(ex);
|
||||
return res.status(500).json({ error: ex });
|
||||
}
|
||||
};
|
||||
24
backend/src/app/controllers/user/routes.ts
Normal file
24
backend/src/app/controllers/user/routes.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { RouteCollection } from "../routes";
|
||||
import create from "./crud/create";
|
||||
import del from "./crud/delete";
|
||||
import login from "./login";
|
||||
|
||||
const routes: RouteCollection[] = [
|
||||
{
|
||||
httpMethod: "PUT",
|
||||
path: "/api/v1/user",
|
||||
handler: create,
|
||||
},
|
||||
{
|
||||
httpMethod: "DELETE",
|
||||
path: "/api/v1/user",
|
||||
handler: del,
|
||||
},
|
||||
{
|
||||
httpMethod: "POST",
|
||||
path: "/api/v1/user/login",
|
||||
handler: login,
|
||||
},
|
||||
];
|
||||
|
||||
export default routes;
|
||||
Loading…
Reference in a new issue