-
-
Notifications
You must be signed in to change notification settings - Fork 365
Expand file tree
/
Copy pathuser.controller.js
More file actions
38 lines (31 loc) · 1.09 KB
/
user.controller.js
File metadata and controls
38 lines (31 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import User from "../models/user.model.js";
import createError from "../utils/createError.js";
export const updateUser = async(req, res, next) => {
const user = await User.findById(req.params.id);
if (req.userId !== user._id.toString()) {
return next(createError(401, "You can update only your account!"));
}
if (req.body.password) {
req.body.password = await bcryptjs.hash(req.body.password, 10);
}
try {
const updatedUser = await User.findByIdAndUpdate(req.params.id, {
$set: req.body
}, { new: true });
res.status(200).json(updatedUser);
} catch (error) {
next(error);
}
}
export const deleteUser = async (req, res, next) => {
const user = await User.findById(req.params.id);
if (req.userId !== user._id.toString()) {
return next(createError(403, "You can delete only your account!"));
}
await User.findByIdAndDelete(req.params.id);
res.status(200).send("deleted.");
};
export const getUser = async (req, res, next) => {
const user = await User.findById(req.params.id);
res.status(200).send(user);
};