Commit 5cae3496 authored by Arthur.Pautrel's avatar Arthur.Pautrel
Browse files

TP10 finished

parent dd31abca
......@@ -45,19 +45,22 @@ router.get('/users', auth.ensureSignedIn, async (req, res) => {
})
router.post('/update-password', auth.ensureSignedIn, auth.currentUser, async (req, res, next) => {
const {newPassword} = req.body;
const {id} = req.params;
const updatedPass = await userService.updatePassword(id, newPassword);
const {password} = req.body;
const { currentUser } = req;
const updatedPass = await userService.updatePassword(currentUser?._id, password);
res.json(updatedPass);
})
router.post('/update-user', auth.ensureSignedIn, async (req, res, next) => {
const {newUsername} = req.body;
router.post('/update-user', auth.ensureSignedIn, auth.currentUser, async (req, res, next) => {
const {username} = req.body;
const { currentUser } = req;
const updatedUsername = await userService.updateUsername(currentUser?._id, username);
res.json(updatedUsername);
})
router.post('/delete-user', auth.ensureSignedIn, async (req, res, next) => {
const { id } = req.params;
const deletedUser = await userService.deleteById();
router.post('/delete-user', auth.ensureSignedIn, auth.currentUser, async (req, res, next) => {
const { currentUser } = req;
const deletedUser = await userService.deleteById(currentUser?._id);
logout(req.session);
res.json(deletedUser);
})
......
......@@ -17,7 +17,6 @@ const register = async (email, username, firstName, lastName, pass, repeatPasswo
}
const createdUser = await Users.create(newUser);
console.log("test");
return {
success: true,
......
......@@ -16,7 +16,7 @@ const findById = async (id) => {
const deleteById = async (id) => {
try {
const retDelete = await Users.deleteOne(id);
const retDelete = await Users.deleteOne({"_id": id});
if (retDelete) {
return {success: true, data: retDelete};
} else {
......@@ -42,6 +42,19 @@ const updatePassword = async (id, newPassword) => {
}
}
const updateUsername = async (id, username) => {
try {
const updateUsername = await Users.updateOne({"_id": id}, {"username": username});
if (updateUsername) {
return {success: true, data: updateUsername};
} else {
return {sucess: false, error: "Failed to change username"};
}
} catch (err) {
return {sucess: false, error: err};
}
}
const getUsers = async () => {
try {
const users = await Users.find();
......@@ -59,5 +72,6 @@ module.exports = {
findById,
getUsers,
deleteById,
updatePassword
updatePassword,
updateUsername
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment