Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exam1 dhlieu2 #67

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/controllers/user.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const updateUserById = async (req, res) => {
const { userId } = req.params;
const newUser = req.body;
try {
const updatedUser = await Blog.findByIdAndUpdate(userId, newUser);
const updatedUser = await User.findByIdAndUpdate(userId, newUser);
if (!updatedUser) {
const err = new Error(" user not found!");
err.status = 404;
Expand All @@ -65,7 +65,7 @@ const updateUserById = async (req, res) => {
const deleteUserById = async (req, res, next) => {
const { userId } = req.params;
try {
const deletedUser = await Blog.findByIdAndDelete(userId);
const deletedUser = await User.findByIdAndDelete(userId);
if (!deletedUser) {
const err = new Error("User not found");
err.status = 400;
Expand Down
35 changes: 35 additions & 0 deletions src/middleware/Auth.middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const User = require('../models/User.model')
const jwt = require('jsonwebtoken')

const authMiddleware = async (req, res, next) =>{
const authorization = req.headers.authorization;
try{
if(!authorization){
const err = new Error("Unauthorizator")
err.status = 401
throw err
}
const token = authorization.split(" ")[1]
const payload = jwt.verify(token,process.env.SECRET_KEY )
const userId = payload.userId
const user = await User.findById(userId)
if(!user) {
const err = new Error("User not exist")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const err = new Error("Unauthorized")

err.status = 404
throw(err)
}
if(user.role !== 'admin'){
const err = new Error("Forbidden!")
err.status = 404
throw(err)
}
req.user = user
next()

}catch(error){
next(error)
}

}

module.exports = authMiddleware;
1 change: 0 additions & 1 deletion src/routers/auth.route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const express = require("express");
const { register, login } = require("../controllers/auth.controller");

const authMiddleware = express.Router();

authMiddleware.route("/register").post(register);
Expand Down
1 change: 0 additions & 1 deletion src/routers/blog.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const storage = multer.diskStorage({
})
const upload = multer({ storage: storage}).single('image');
const blogRouter = express.Router();

blogRouter.route("/").get(getBlogs).post(upload, createBlog);

blogRouter.route("/:blogId").get(getBlog).put(updateBlog).delete(deleteBlog);
Expand Down
8 changes: 4 additions & 4 deletions src/routers/user.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const {
deleteUserById,
} = require("../controllers/User.controllers");
const userRouter = express.Router();

userRouter.route("/").get(getUsers).post(createUser);
const authMiddleware = require('../middleware/Auth.middleware')
userRouter.route("/").get(getUsers).post(authMiddleware,createUser);

userRouter
.route("/:userId")
.get(getUserById)
.put(updateUserById)
.delete(deleteUserById);
.put(authMiddleware,updateUserById)
.delete(authMiddleware,deleteUserById);

module.exports = userRouter;