Skip to content

Commit

Permalink
edit routes works
Browse files Browse the repository at this point in the history
  • Loading branch information
JaberHPranto committed Jul 10, 2021
1 parent 47d2dc9 commit 5789299
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
38 changes: 37 additions & 1 deletion server/controller/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,52 @@ export const resetPassword = async (req, res) => {
export const getUsers = asyncHandler(async (req, res) => {
const users = await User.find()
res.json(users)

});

// @ delete a user
export const deleteUser = asyncHandler(async (req, res) => {

const user = await User.findById(req.params.id)
if (user) {
await user.remove()
res.status(200).json({ message: "User removed" })
} else {
res.status(404)
throw new Error("User not found")
}
});

// @ get a user
export const getUserById = asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id)
if (user) {
res.status(200).json(user)
} else {
res.status(404)
throw new Error("User not found")
}
});

// @ update a user
export const updateUser = asyncHandler(async (req, res) => {
const user = await User.findById(req.params.id);

if (user) {
user.name = req.body.name || user.name;
user.email = req.body.email || user.email;
user.isAdmin = req.body.isAdmin

const updatedUser = await user.save();

res.status(200).json({
_id: updatedUser._id,
name: updatedUser.name,
email: updatedUser.email,
isAdmin:updatedUser.isAdmin
});
}
else {
res.status(401);
throw new Error("Not authorized to view profile");
}
});
2 changes: 1 addition & 1 deletion server/middlewares/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const isLoggedIn = async (req, res, next) => {
if (token && isCustomAuth) {
decodedData = jwt.verify(token, process.env.JWT_SECRET)
req.userId = decodedData?.id
req.user = await User.findById(req.userId)
req.user = await User.findById(req.userId).select('-password')
}
else {
// for google authentication
Expand Down
8 changes: 5 additions & 3 deletions server/routes/userRoutes.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import express from 'express'
import { authUser, deleteUser, forgetPassword, getUserProfile, getUsers, registerUser, resetPassword, updateUserProfile } from '../controller/userController.js'
import { authUser, deleteUser, forgetPassword, getUserById, getUserProfile, getUsers, registerUser, resetPassword, updateUser, updateUserProfile } from '../controller/userController.js'
import { isAdmin, isLoggedIn } from '../middlewares/authMiddleware.js'

const router = express.Router()

router.get("/", isLoggedIn, isAdmin, getUsers)
router.delete("/:id",isLoggedIn,isAdmin,deleteUser)
router.delete("/:id",isLoggedIn, isAdmin, deleteUser)
router.get("/:id",isLoggedIn, isAdmin, getUserById)
router.put("/:id",isLoggedIn,isAdmin,updateUser)

router.post("/login", authUser)
router.post("/register",registerUser)
router.route("/profile").get(isLoggedIn, getUserProfile).put(isLoggedIn, updateUserProfile)
router.post("/forget-password", forgetPassword)
router.put("/reset-password/:resetToken",resetPassword)
router.route("/profile").get(isLoggedIn, getUserProfile).put(isLoggedIn, updateUserProfile)



Expand Down

0 comments on commit 5789299

Please sign in to comment.