diff --git a/backend/api/controllers/user.js b/backend/api/controllers/user.js index 95785c753..c26c592b4 100644 --- a/backend/api/controllers/user.js +++ b/backend/api/controllers/user.js @@ -18,10 +18,20 @@ class UserController { } } + static async deleteUser(req, res, next) { + try { + const { uid } = req.body; + await UsersDAO.deleteUser(uid); + return res.sendStatus(200); + } catch (e) { + return next(e); + } + } + static async updateName(req, res, next) { try { const { name } = req.body; - if (!isUsernameValid(name)) return next("Username invalid!"); + if (!isUsernameValid(name)) return res.status(400).json({message:"Username invalid. Name cannot contain special characters or contain more than 14 characters. Can include _ . and -"}); await UsersDAO.updateName(); return res.sendStatus(200); } catch (e) { @@ -32,9 +42,9 @@ class UserController { static async checkName(req, res, next) { try { const { name } = req.body; - if (!isUsernameValid(name)) return next("Username invalid"); + if (!isUsernameValid(name)) return next({status: 400, message: "Username invalid. Name cannot contain special characters or contain more than 14 characters. Can include _ . and -"}); const available = await UsersDAO.isNameAvailable(name); - if(!available) return next("Username unavailable"); + if(!available) return res.status(400).json({message:"Username unavailable"}); return res.sendStatus(200); } catch (e) { return next(e); diff --git a/backend/api/routes/user.js b/backend/api/routes/user.js index 5df2b6c59..ebff57eec 100644 --- a/backend/api/routes/user.js +++ b/backend/api/routes/user.js @@ -5,9 +5,11 @@ const UserController = require("../controllers/user"); const router = Router(); -router.post("/user/signup", UserController.createNewUser); +router.post("/signup", UserController.createNewUser); -router.post("/user/checkName", authenticateRequest, UserController.checkName); +router.post("/checkName", UserController.checkName); + +router.post("/delete", UserController.deleteUser); router.post("/user/updateName", authenticateRequest, UserController.updateName); diff --git a/backend/dao/user.js b/backend/dao/user.js index 2e88a9474..9b85377dc 100644 --- a/backend/dao/user.js +++ b/backend/dao/user.js @@ -10,6 +10,12 @@ class UsersDAO { .insertOne({ name, email, uid, addedAt: Date.now() }); } + static async deleteUser(uid) { + return await mongoDB() + .collection("users") + .deleteOne({uid}); + } + static async updateName(uid, name) { const nameDoc = await mongoDB() .collection("users")