Skip to content

Commit

Permalink
[BE][Feat] #219 : 헤더의 JWT 토큰으로 사용자 검증 middleware 추가
Browse files Browse the repository at this point in the history
- 헤더의 JWT 토큰으로 사용자 검증 middleware 추가
  • Loading branch information
happyhyep committed Nov 21, 2024
1 parent 02bef4e commit 48aa725
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions backend/src/middleware/authenticateJWT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import jwt from 'jsonwebtoken';

export const authenticateJWT = (req, res, next) => {
const token = req.header('Authorization')?.replace('Bearer ', '');

if (!token) {
return res.status(403).json({ message: 'Access Denied: No Token Provided' });
}

jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ message: 'Invalid Token' });
}

req.user = user;
next();
});
};
5 changes: 5 additions & 0 deletions backend/src/routes/channelRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getUserChannelsController,
} from '../controllers/channelController.js';
import { validationMiddleware } from '../middleware/validationMiddleware.js';
import { authenticateJWT } from '../middleware/authenticateJWT.js';

export const channelRouter = express.Router();

Expand Down Expand Up @@ -40,6 +41,7 @@ channelRouter.post(
body('name').notEmpty().withMessage('Name is required'),
body('host_id').notEmpty().withMessage('Host ID is required'),
],
authenticateJWT,
validationMiddleware,
createChannelController,
);
Expand Down Expand Up @@ -77,6 +79,7 @@ channelRouter.post(
channelRouter.post(
'/:channelId/guests',
[body('guests').isArray().withMessage('Guests must be an array')],
authenticateJWT,
validationMiddleware,
addGuestController,
);
Expand Down Expand Up @@ -108,6 +111,7 @@ channelRouter.post(
channelRouter.get(
'/:id',
[param('id').notEmpty().withMessage('Channel ID is required')],
authenticateJWT,
validationMiddleware,
getChannelInfoController,
);
Expand Down Expand Up @@ -182,6 +186,7 @@ channelRouter.get(
channelRouter.get(
'/user/:userId',
[param('userId').notEmpty().withMessage('User ID is required')],
authenticateJWT,
validationMiddleware,
getUserChannelsController,
);

0 comments on commit 48aa725

Please sign in to comment.