2022-04-05 01:15:43 +00:00
|
|
|
import express from "express";
|
2022-04-07 22:56:04 +00:00
|
|
|
import validator from "validator";
|
|
|
|
import { requireAdmin, requireAuthenticated } from "../middleware/authority.js";
|
2022-04-05 01:15:43 +00:00
|
|
|
import { needDatabase } from "../middleware/database.js";
|
2022-04-05 03:50:26 +00:00
|
|
|
import userModel from "../schemas/userModel.js";
|
2022-04-05 01:15:43 +00:00
|
|
|
import User from "../schemas/userModel.js";
|
|
|
|
const UserController = express.Router();
|
|
|
|
|
|
|
|
UserController.post("/login", needDatabase, async (req, res) => {
|
|
|
|
try {
|
|
|
|
const email = req.body.email;
|
|
|
|
const pwd = req.body.password;
|
|
|
|
const user = await User.credentialsExist(email, pwd);
|
|
|
|
if (!user) {
|
|
|
|
res.sendStatus(401);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
req.session.userId = user._id;
|
|
|
|
req.session.email = user.email;
|
2022-04-07 03:53:20 +00:00
|
|
|
user.password = undefined;
|
|
|
|
res.status(200).send(user);
|
2022-04-05 01:15:43 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === "TypeError") {
|
|
|
|
res.status(400).send("Missing required user info.");
|
|
|
|
} else if (error.message === "Credentials do not exist.") {
|
|
|
|
res.status(401).send("Credentials do not exist.");
|
|
|
|
} else {
|
|
|
|
console.error(error);
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
res.status(500).send(error.toString());
|
|
|
|
} else {
|
|
|
|
res.status(500).send("Internal server error. This issue has been noted.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-05 16:50:35 +00:00
|
|
|
UserController.get("/logout", requireAuthenticated, (req, res) => {
|
2022-04-05 01:15:43 +00:00
|
|
|
req.session.destroy((err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
res.status(500).send(err.toString());
|
|
|
|
} else {
|
|
|
|
res.status(500).send("Internal server error. This issue has been noted.");
|
|
|
|
}
|
|
|
|
res.status(500).send("");
|
|
|
|
} else {
|
|
|
|
res.sendStatus(200);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-04-05 16:50:35 +00:00
|
|
|
UserController.get("/:id?", needDatabase, requireAuthenticated, async (req, res) => {
|
2022-04-05 03:50:26 +00:00
|
|
|
let user = null;
|
|
|
|
if (req.params.id) {
|
|
|
|
if (req.user.accessLevel > 2) {
|
|
|
|
user = await userModel.findById(req.params.id);
|
|
|
|
} else {
|
|
|
|
res.status(401).send("Unauthorized.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
} else {
|
2022-04-05 03:50:26 +00:00
|
|
|
user = req.user;
|
2022-04-05 01:15:43 +00:00
|
|
|
}
|
2022-04-05 03:50:26 +00:00
|
|
|
user.password = undefined;
|
|
|
|
res.status(200).send(user);
|
2022-04-05 01:15:43 +00:00
|
|
|
});
|
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
|
2022-04-05 16:50:35 +00:00
|
|
|
UserController.patch("/:id?", needDatabase, requireAuthenticated, async (req, res) => {
|
2022-04-07 22:56:04 +00:00
|
|
|
try {
|
|
|
|
let user = null;
|
|
|
|
if (req.params.id) {
|
|
|
|
if (req.user.accessLevel > 2) {
|
|
|
|
user = await userModel.findById(req.params.id);
|
|
|
|
} else {
|
|
|
|
res.status(401).send("Unauthorized.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 03:50:26 +00:00
|
|
|
} else {
|
2022-04-07 22:56:04 +00:00
|
|
|
user = req.user;
|
|
|
|
}
|
|
|
|
if (req.body._id) {
|
|
|
|
res.status(400).send("Cannot change user ID.");
|
2022-04-05 03:50:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
if (req.body.createdMatches) {
|
|
|
|
res.status(400).send("Cannot directly change the list of created matches.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
if (req.body.password) {
|
|
|
|
res.status(400).send("Cannot directly change user password.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
if (req.body.participatingMatches) {
|
|
|
|
res.status(400).send("Cannot directly change the list of participating matches.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
if (req.body.joinDate) {
|
|
|
|
res.status(400).send("Cannot change the join date.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.accessLevel && req.user.accessLevel < 3) {
|
|
|
|
res.status(401).send("Unauthorized to change the access level of this user.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.body.suspend && req.user.accessLevel < 3) {
|
|
|
|
res.status(401).send("Unauthorized to change the accounts disabled date. ");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await user.updateOne(req.body);
|
|
|
|
res.status(200).send("Updated.");
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).send("Internal server error");
|
2022-04-05 03:50:26 +00:00
|
|
|
}
|
2022-04-07 22:56:04 +00:00
|
|
|
});
|
2022-04-05 03:50:26 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
UserController.get("/all/active", requireAdmin, async (req, res) => {
|
|
|
|
try {
|
|
|
|
if (req.user.accessLevel < 3) {
|
|
|
|
res.status(401).send("You do not have the required privileges.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let res = await userModel.find().where("suspend").lt(Date.now);
|
|
|
|
res.status(200).send({ all: res });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).send("Internal server error");
|
2022-04-05 01:15:43 +00:00
|
|
|
}
|
2022-04-07 22:56:04 +00:00
|
|
|
});
|
2022-04-05 03:50:26 +00:00
|
|
|
|
2022-04-07 22:56:04 +00:00
|
|
|
UserController.get("/all/suspended", requireAuthenticated, async (req, res) => {
|
|
|
|
try {
|
|
|
|
let res = await userModel.find().where("suspend").gte(Date.now);
|
|
|
|
res.status(200).send({ suspended: res });
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
res.status(500).send("Internal server error");
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
});
|
|
|
|
|
2022-04-05 03:50:26 +00:00
|
|
|
/* TODO: Implement middleware for removing users.
|
|
|
|
|
2022-04-05 16:50:35 +00:00
|
|
|
UserController.delete("/:id?", needDatabase, requireAuthenticated, async (req, res) => {
|
2022-04-05 03:50:26 +00:00
|
|
|
let user = null;
|
|
|
|
if (req.params.id) {
|
|
|
|
if (req.user.accessLevel > 2) {
|
|
|
|
user = await userModel.findById(req.params.id);
|
|
|
|
} else {
|
|
|
|
res.status(401).send("Unauthorized.");
|
|
|
|
return;
|
|
|
|
}
|
2022-04-05 01:15:43 +00:00
|
|
|
} else {
|
2022-04-05 03:50:26 +00:00
|
|
|
user = req.user;
|
2022-04-05 01:15:43 +00:00
|
|
|
}
|
2022-04-05 03:50:26 +00:00
|
|
|
|
|
|
|
await user.deleteOne();
|
|
|
|
res.status(200).send("Deleted user.");
|
2022-04-05 01:15:43 +00:00
|
|
|
});
|
|
|
|
|
2022-04-05 03:50:26 +00:00
|
|
|
*/
|
2022-04-05 01:15:43 +00:00
|
|
|
|
|
|
|
UserController.post("/", needDatabase, async (req, res) => {
|
|
|
|
try {
|
2022-04-07 22:56:04 +00:00
|
|
|
const data = {
|
2022-04-05 01:15:43 +00:00
|
|
|
email: req.body.email,
|
|
|
|
firstName: req.body.firstName,
|
|
|
|
lastName: req.body.lastName,
|
|
|
|
phone: req.body.phone,
|
|
|
|
password: req.body.password,
|
2022-04-07 22:56:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let createdUser = new User(data);
|
2022-04-05 01:15:43 +00:00
|
|
|
await createdUser.save();
|
|
|
|
res.sendStatus(201);
|
|
|
|
return;
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name === "TypeError" || err.name === "ValidationError") {
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
console.error(err);
|
|
|
|
res.status(400).send(err.toString());
|
|
|
|
} else {
|
|
|
|
res.status(400).send("Missing required user info.");
|
|
|
|
}
|
|
|
|
} else if (err.name === "MongoServerError" && err.message.startsWith("E11000")) {
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
console.error(err);
|
|
|
|
res.status(409).send(err.toString());
|
|
|
|
} else {
|
|
|
|
res.status(409).send("User already exists.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.error(err);
|
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
res.status(500).send(err.toString());
|
|
|
|
} else {
|
|
|
|
res.status(500).send("Internal server error. This issue has been noted.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default UserController;
|