jwt관련 파일 설치하고
npm install jsonwebtoken
다음과 같이 간단히 jwt를 발급할 수 있다.
const jwt = require('jsonwebtoken');
const SECRET_KET = "your secret key";
jwtToken = jwt.sign({
social_id : response.data.id,
social_type : 1,
image_url: response.data.kakao_account.profile.profile_image_url,
nickname: response.data.kakao_account.profile.nickname
}, SECRET_KEY, {
expiresIn: '1h'
});
secret key는 본문에 적으면 안되고 .env파일과 같은 곳에 숨겨 두어야 한다.
프론트단에서는 전달받은 jwt를 다음과 같이 풀 수 있다.
jwt 관련 파일 설치하고
npm install jwt-decode
const decodedToken = jwtDecode(response1.data.token);
response1.data.token부분에 백으로부터 전달받은 jwt가 들어간다.
이 코드는 jwt를 검증하는 코드가 아니라 jwt를 푸는 코드다