make it bett

This commit is contained in:
pawan-dot 2022-06-24 14:03:47 +05:30
parent 463dce8695
commit 081769c0e2
2 changed files with 23 additions and 2 deletions

View File

@ -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) => { export const updatePassword = catchAsyncErrors(async (req, res, next) => {
const user = await User.findById(req.user.id).select("+password"); 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); sendToken(user, 200, res);
}); });
// 8.update User Profile // 9.update User Profile
export const updateProfile = catchAsyncErrors(async (req, res, next) => { export const updateProfile = catchAsyncErrors(async (req, res, next) => {
const newUserData = { const newUserData = {
name: req.body.name, name: req.body.name,

View File

@ -8,6 +8,7 @@ import {
getUserDetails, getUserDetails,
updatePassword, updatePassword,
updateProfile, updateProfile,
getSingleUser
} from "../controllers/userController.js" } from "../controllers/userController.js"
import { isAuthenticatedUser, authorizeRoles } from "../middlewares/auth.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/logout").get(logout);
router.route("/user/details").get(isAuthenticatedUser, getUserDetails); 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); router.route("/user/password/update").put(isAuthenticatedUser, updatePassword);