forked from EwhaSpeakUP/SpeakUP_Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
36 lines (31 loc) · 871 Bytes
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const jwt = require("jsonwebtoken");
const jwtsecret = require('./config/secret_config').jwtsecret;
const authMiddleware = (req, res, next) => {
const token = req.headers["access_token"] || req.query.token;
console.error("사용자가 전송한 토큰:",token);
if (!token) {
return res.json({
isSuccess: false,
code: 403,
message: "로그인되지 않은 상태입니다.",
});
}
const p = new Promise((resolve, reject) => {
jwt.verify(token, jwtsecret, (err, decoded) => {
if (err) reject(err);
resolve(decoded);
});
});
const onError = (error) => {
return res.json({
isSuccess: false,
code: 403,
message: "유효하지 않은 토큰입니다.",
});
};
p.then((decoded) => {
req.decoded = decoded;
next();
}).catch(onError);
};
module.exports = authMiddleware;