Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- lightsail nodejs apache
- OOP
- 객체참조 #객체
- this
- NPM
- url #querystring
- 기후변화
- git pair
- TypeScript
- ESLint
- jest
- #cloudfront #s3 #html 확장자 없애기
- 클로저
Archives
- Today
- Total
Hello World...
jwt token 본문
app.js
const express = require('express');
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const app = express();
const secretObj = require('./config');
app.use(cookieParser());
app.get('/', (req, res) => {
res.send('hello~');
});
app.get('/jwt', (req, res) => {
try {
const token = jwt.sign(
{
email: 'user@example.com'
},
secretObj.secret,
{
expiresIn: '2h'
}
);
res.cookie('user', token);
res.json({ token });
} catch (error) {
console.log(error);
res.send('다시 시작해주세요');
}
});
app.get('/type', (req, res) => {
console.log(req.cookies);
try {
const token = req.cookies.user;
const decoded = jwt.verify(token, secretObj.secret);
if (decoded) {
res.send('type form 보여주기');
} else {
res.send('권한이 없습니다');
}
} catch (error) {
console.log(error);
res.send('권한이 없습니다');
}
});
app.listen(5001, () => {
console.log('port 5001 server running...');
});
config.js
const jwtObj = {};
jwtObj.secret = 'secretCode';
module.exports = jwtObj;
참고)
https://victorydntmd.tistory.com/116
[Node.js] JWT 기반으로 사용자 인증 구현하기 ( jsonwebtoken 모듈 )
2019. 06. 22 수정 1. JWT ( Json Web Token ) 많은 웹 서비스들은 사용자 인증을 구현하기 위해서 쿠키와 세션을 이용해왔습니다. 그런데 쿠키와 세션에는 여러 문제들이 있어서, 최근에는 OAuth와 JWT 같은
victorydntmd.tistory.com
'node.js' 카테고리의 다른 글
[코작] BookNote 제작하기 (0) | 2020.04.16 |
---|---|
[코작作] nodejs 콜백(callback) 비동기 처리 공부! (0) | 2020.02.16 |
nodejs - mysql 간단한 연동 공부 (0) | 2020.02.11 |
nodemon , lowdb 관련 계속 실행 문제 (0) | 2020.01.17 |
nodejs lowdb 테스트 중 return (0) | 2020.01.17 |
Comments