-
Notifications
You must be signed in to change notification settings - Fork 1
/
logged.js
38 lines (34 loc) · 885 Bytes
/
logged.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
37
38
const jwt = require('jsonwebtoken')
var tokenKey = "secretKey"
const authMiddleware = (req, res, next) => {
const token = req.headers['token'] || req.query.token;
console.error(token)
if(!token) {
return res.status(403).json({
server : "서버",
success: false,
message: 'not logged in'
})
}
const p = new Promise(
(resolve, reject) => {
jwt.verify(token, tokenKey, (err, decoded) => {
if(err) reject(err)
resolve(decoded)
})
}
)
const onError = (error) => {
console.log(error);
res.status(403).json({
server : "서버",
success: false,
message: error.message
})
}
p.then((decoded)=>{
req.decoded = decoded
next()
}).catch(onError)
}
module.exports = authMiddleware;