-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
23 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const SECRET_KEY = 'piro'; // 나중에 환경 파일 | ||
const jwt = require("jsonwebtoken"); | ||
const SECRET_KEY = "piro"; // 나중에 환경 파일 | ||
|
||
// JWT 검증 미들웨어 | ||
const authenticateToken = (req, res, next) => { | ||
const authHeader = req.headers['authorization']; | ||
const token = authHeader && authHeader.split(' ')[1]; | ||
const authHeader = req.headers["authorization"]; | ||
const token = authHeader && authHeader.split(" ")[1]; | ||
|
||
if (token == null) return res.sendStatus(401); // 토큰 없음 | ||
if (token == null) return res.sendStatus(401); // 토큰 없음 | ||
|
||
jwt.verify(token, SECRET_KEY, (err, user) => { | ||
if (err) return res.sendStatus(403); // 토큰 무효 | ||
req.user = user; // 이 이후에는 req.user가 user 임 | ||
next(); | ||
}); | ||
jwt.verify(token, SECRET_KEY, (err, user) => { | ||
if (err) return res.sendStatus(403); // 토큰 무효 | ||
req.user = user; // 이 이후에는 req.user가 user 임 | ||
next(); | ||
}); | ||
}; | ||
|
||
module.exports = authenticateToken; | ||
module.exports = authenticateToken; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters