-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthtoken.js
49 lines (41 loc) · 1.39 KB
/
authtoken.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
39
40
41
42
43
44
45
46
47
48
49
const { checkToken } = require("token.js");
exports.authtoken = async (req, res, next) => {
const headers = req.headers;
const authorization = headers.authorization;
const [accessToken, refreshToken] = authorization.split(",");
//-------- check token & get user id --------------------------------------------------------------------------------------//
let tokenCheck;
let returnBody;
let id;
try {
tokenCheck = await checkToken(accessToken, refreshToken);
returnBody = JSON.parse(tokenCheck.body);
id = returnBody.tokenData.id;
} catch (error) {
//return invalid when token is invalid
console.log("authtoken 에서 에러가 발생했습니다.");
const return_body = {
success: false,
detail_code: "99",
message: "login again",
returnToken: null,
};
return res.status(401).send(return_body);
}
//return invalid when token is invalid
if (tokenCheck.statusCode !== 200) {
console.log("authtoken 에서 에러가 발생했습니다.");
const return_body = {
success: false,
detail_code: "99",
message: "login again",
returnToken: null,
};
return res.status(401).send(return_body);
}
const returnToken = returnBody.accessToken;
//-------- sucess result --------------------------------------------------------------------------------------//
req.id = id;
req.returnToken = returnToken;
next();
};