diff --git a/controllers/userController.js b/controllers/userController.js index 4548488..ac79f0e 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -171,7 +171,22 @@ export const getUserDetails = catchAsyncErrors(async (req, res, next) => { }); }); -// 7.update User password +// 7.Get single user (admin) +exports.getSingleUser = catchAsyncErrors(async (req, res, next) => { + const user = await User.findById(req.params.id); + + if (!user) { + return next( + new ErrorHander(`User does not exist with Id: ${req.params.id}`) + ); + } + + res.status(200).json({ + success: true, + user, + }); +}); +// 8.update User password export const updatePassword = catchAsyncErrors(async (req, res, next) => { const user = await User.findById(req.user.id).select("+password"); @@ -192,7 +207,7 @@ export const updatePassword = catchAsyncErrors(async (req, res, next) => { sendToken(user, 200, res); }); -// 8.update User Profile +// 9.update User Profile export const updateProfile = catchAsyncErrors(async (req, res, next) => { const newUserData = { name: req.body.name, diff --git a/routes/userRoute.js b/routes/userRoute.js index bc63b72..379ce24 100644 --- a/routes/userRoute.js +++ b/routes/userRoute.js @@ -8,6 +8,7 @@ import { getUserDetails, updatePassword, updateProfile, + getSingleUser } from "../controllers/userController.js" import { isAuthenticatedUser, authorizeRoles } from "../middlewares/auth.js" @@ -24,6 +25,11 @@ router.route("/user/password/reset/:token").put(resetPassword); router.route("/user/logout").get(logout); router.route("/user/details").get(isAuthenticatedUser, getUserDetails); +router + .route("/admin/user/:id") + .get(isAuthenticatedUser, authorizeRoles("admin"), getSingleUser) + + router.route("/user/password/update").put(isAuthenticatedUser, updatePassword);