require('dotenv').config();
const { sign, verify } = require('jsonwebtoken');
module.exports = {
generateAccessToken: (data) => {
return sign(data, process.env.ACCESS_SECRET, { expiresIn: '2d' });
},
sendAccessToken: (res, accessToken) => {
res.json({ data: { accessToken }, message: "ok" });
},
resendAccessToken: (res, accessToken, data) => {
res.json({ data: { accessToken, userInfo: data }, message: "ok" });
},
isAuthorized: (req) => {
const authorization = req.headers["authorization"];
if (!authorization) {
return null;
}
const token = authorization.split(" ")[1];
console.log('token', token)
try {
return verify(token, process.env.ACCESS_SECRET);
} catch (err) {
return null;
}
}
};
require('dotenv').config();
const { sign, verify } = require('jsonwebtoken');
module.exports = {
generateAccessToken: (data) => {
return sign(data, process.env.ACCESS_SECRET, { expiresIn: '2d' });
},
sendAccessToken: (res, accessToken) => {
console.log('accessToken', accessToken)
return res
.status(200)
.cookie('jwt', accessToken)
.json({ data: { accessToken }, message: 'OK' });
},
isAuthorized: (req) => {
const cookie = req.headers.cookie;
console.log('cookie 🚌', cookie)
if (!cookie) {
return null;
} else {
const token = cookie.split(" ")[0]
// console.log('token 🏀', token)
const tokenSlice = token.slice(4, token.length)
// console.log('tokenSlice 🏀', tokenSlice)
const data = verify(tokenSlice, process.env.ACCESS_SECRET);
// console.log('data 🏀', data)
if (!data) {
return null
} else {
return data;
}
}
}
};
postman에 저장되어있던 쿠키까지 나와서 그렇게 된 것,
postman 초기화 후 재시도 하면 정상 작동한다