회원정보수정 - PUT
checkToken
이라는 과정에서 토큰을 검증한다.
: router.put('/mypage', checkToken, async function(req, res, next)
const checkToken = require('../config/auth').checkToken;
: checkToken
은 ../config/auth
에 있는 checkToken
을 가져와서 쓴다.
next();
의 사용next();
가 들어있는 checkToken
에서 토큰검증 후 req로 넘긴다.console.log('1. req.headers =>',req.headers);
const token = req.headers.token;
token
으로 정의함.1. 토큰이 있느냐
if(!token){ return res.send({status:888, result:'유효하지않은 토큰'}) }
2. 토큰 decode(해독,꺼내는거,추출)
const user = jwt.verify(token, self.securityKey);
if(typeof user.uid === 'undefined'){ return res.send({status:888, result:'유효하지않은 토큰'}) }
user.uid
)가 없으면(undefined
), 888 뜨고 유효하지않은 토큰이라고 뜸3. 추출 완료
토큰에서 추출한 id
: console.log('2. 토큰에서 추출한 id =>', user.uid);
auth.js의 req.body
: console.log('2-1. auth.js의 req.body =>',req.body);
4. 추출한 uid 첨부
req.body.uid = user.uid;
5. next();
6. member.js 확인
console.log('3. member.js로 무사히 넘어왔음 =>', req.body);
전체 로그
1. auth.js => req.headers => {
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJheiIsImlhdCI6MTY0MTM1MTIzMiwiZXhwIjoxNjQxMzgzNjMyLCJpc3MiOiJjb3JwMDEifQ.wtaBllh3yOgZKCJKkccHUBF5xCR_smjsLmwUPe9g8Fw',
'content-type': 'application/json',
'user-agent': 'PostmanRuntime/7.28.4',
accept: '*/*',
'cache-control': 'no-cache',
'postman-token': '5a204233-539c-4478-a563-741ddc7d1027',
host: 'localhost:3000',
'accept-encoding': 'gzip, deflate, br',
connection: 'keep-alive',
'content-length': '31'
}
2. auth.js => token = req.headers.token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJheiIsImlhdCI6MTY0MTM1MTIzMiwiZXhwIjoxNjQxMzgzNjMyLCJpc3MiOiJjb3JwMDEifQ.wtaBllh3yOgZKCJKkccHUBF5xCR_smjsLmwUPe9g8Fw
3. auth.js => 토큰에서 추출한 id => az
4. auth.js => req.body => { uage: 13, uemail: 'b@b.com' }
-----------------------------------------------------
5. member.js => 무사히 넘어왔음 req.body => { uage: 13, uemail: 'b@b.com', uid: 'az' }