jwtMiddleware.js
2.34 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const jwt = require("jsonwebtoken");
const User = require("../models/user");
/*function generateToken(payload) {
return new Promise((resolve, reject) => {
jwt.sign(
payload,
jwtSecret,
{
expiresIn: '7d' // 토큰 유효기간 7일 설정
}, (error, token) => {
if (error) reject(error);
resolve(token);
}
);
}
);
};
function decodeToken(token) {
return new Promise(
(resolve, reject) => {
jwt.verify(token, jwtSecret, (error, decoded) => {
if(error) reject(error);
resolve(decoded);
});
}
);
}
const jwtMiddleware = async (ctx, next) => {
const token = ctx.cookies.get("access_token");
if (!token) {
return next();} //token이 없음
try {
const decoded = await decodeToken(token); // 토큰을 디코딩 합니다
const user = await User.findById(decoded._id).exec();
ctx.state.user = {
_id: decoded._id,
email: decoded.email
};
//특정 시간 남았으면 재발급
const now = Math.floor(Date.now() / 1000);
if (decoded.exp - now < 60 * 60 * 24 * 3.5) {
const user = await User.findById(decoded._id);
const token = user.generateToken();
ctx.cookies.set("access_token", token, {
maxAge: 1000 * 60 * 60 * 24 * 7, // 7일
httpOnly: true
});
}
ctx.request.user = decoded;
return next();
} catch (e) {
//검증 실패
return next();
}
};
*/
const jwtMiddleware = async (ctx, next) => {
const token = ctx.cookies.get("access_token");
if (!token) {
return next();} //token이 없음
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findById(decoded._id).exec();
ctx.state.user = {
_id: decoded._id,
email: decoded.email
};
//특정 시간 남았으면 재발급
const now = Math.floor(Date.now() / 1000);
if (decoded.exp - now < 60 * 60 * 24 * 3.5) {
const user = await User.findById(decoded._id);
const token = user.generateToken();
ctx.cookies.set("access_token", token, {
maxAge: 1000 * 60 * 60 * 24 * 7, // 7일
httpOnly: true,
});
}
return next();
} catch (e) {
//검증 실패
return next();
}
};
module.exports = jwtMiddleware;