member.js
router.post('/select', async function(req, res, next) {
try {
console.log('req.body =>', req.body);
// 회원가입시 사용했던 암호화방식으로 hash 해야
// DB에서 로그인 비교가 가능함
const hash = crypto.createHmac('sha256', req.body.uid )
.update(req.body.upw)
.digest('hex');
const obj = {
userid : req.body.uid,
userpw : hash
};
console.log('obj =>', obj);
const dbConn = await db.connect(DBURL);
const coll = dbConn.db(DBNAME).collection("member");
// const query = {$and : [{ _id : obj.userid}, {userpw : obj.userpw}]}
const query = { _id:obj.userid, upw:obj.userpw };
// _id와 upw는 DB와 같아야한다.
console.log('query =>',query);
const result = await coll.findOne(query);
console.log('result =>', result); // 일치할경우, 일치하지않을경우 로그로 확인
if(result !== null){ // DB에 일치하는 경우
const token = {
token : jwt.sign(
{uid : result._id}, // 토큰에 포함할 내용들
jwtKey, // 토큰생성 키
jwtOptions // 옵션
),
refreshToken : null, // null
}
return res.send({status:200, result:token});
}
// 일치하지 않는 경우
return res.send({status:0});
}
catch (err) {
console.error(err);
return res.send({status:-1, result : err});
}
});
Login.vue
async handleLogin(){
if(this.uid === ''){
alert('아이디를 입력하세요.');
this.$refs.id.focus();
return false;
}
if(this.upw === ''){
alert('암호를 입력하세요.');
this.$refs.pw.focus();
return false;
}
// 로그인 : localhost:3000/member/select
const url = '/member/select';
const headers = {"Content-Type":"application/json"};
const body = {
uid : this.uid,
upw : this.upw
}
const response = await this.axios.post(url, body, headers);
console.log(response);
// this.token = '12423k1g524...생략...5iou123hkbt51b';
// sessionStorage.setItem("TOKEN", this.token);
if(response.data.status === 200){
alert('로그인 되었습니다.');
this.token = response.data.result.token;
console.log(this.token);
sessionStorage.setItem("TOKEN", this.token);
}
else if(response.data.status === 0){
alert('아이디 또는 암호를 확인하세요.')
}
// this.store.commit('setMenu', 'home');
// 부모 컴포넌트로 이벤트를 발생시킴
// 이벤트명은 changeLogged('home')
// this.$emit('changeLogged','home');
}