회원가입 - POST
1. 백엔드 - member.js
// localhost:3000/member/
// 회원가입 : localhost:3000/member/insert //app.js에 있는명칭
router.post('/insert', async function(req, res, next) {
try {
console.log('req.body =>', req.body);
// salt
// hash(salt)는 abc -> 2398ohtgklas98a0h4wtlkiha
// hash(salt)는 abc -> 2398ohtgklas98a0h4wtlkiha
// req.body.upw
const hash = crypto.createHmac('sha256', req.body.uid )
.update(req.body.upw)
.digest('hex');
// req.body.uid = salt값 (고유한 값을 사용)
const obj = {
// 백에서 DB로 던지는 상태변수명
// 프론트에서 백으로 던지는것과는 다름
_id : req.body.uid,
upw : hash, // req.body.upw,
uage : Number(req.body.uage),
uname : req.body.uname,
ubirth : req.body.ubirth,
uemail : req.body.uemail,
ucheck : req.body.ucheck,
ugender : Number(req.body.ugender),
};
console.log('obj =>', obj);
const dbConn = await db.connect(DBURL);
const coll = dbConn.db(DBNAME).collection("member");
const result = await coll.insertOne(obj);
console.log(result);
if(result.insertedId === obj._id, result.acknowledged === true){
return res.send({status:200});
}
return res.send({status:0});
}
catch (err) {
console.error(err);
return res.send({status:-1, result : err});
}
});
2. 프론트엔드 - Join.vue
// 유효성 검사 후
const url = `/member/insert`;
const headers = {"Content-Type":"application/json"};
const body = {
uid : this.member.userid,
upw : this.member.userpw,
uage : this.member.userage,
uname : this.member.username,
ubirth : this.member.userdate,
uemail : this.member.usermail + "@" + this.member.dom,
ucheck : JSON.stringify(this.member.type),
ugender : this.member.usergender,
};
const response = await this.axios.post(url, body, {headers:headers});
console.log(response);
if(response.data.status === 200){
alert('회원가입되었습니다');
this.$router.push({name:'Home'})
}
아이디 중복검사
1. 백엔드 - member.js - GET
router.get('/idcheck', async function(req, res, next) {
try {
console.log('req =>', req);
const userid = req.query.uid;
const dbConn = await db.connect(DBURL);
const coll = dbConn.db(DBNAME).collection("member");
const query = {_id : userid};
const result = await coll.countDocuments(query);
console.log(result);
// 같은아이디 있으면 result:1
// 없으면 result : 0
return res.send({status:200, result:result});
}
catch (err) {
console.error(err);
return res.send({status:-1, result : err});
}
});
2. 프론트엔드 - Join.vue - GET
// 아이디중복확인 : localhost:3000/member/idcheck?uid=aa
async IdCheck(){
console.log('Join.vue => IdCheck');
console.log('Join.vue => IdCheck', this.member.userid);
const url = `/member/idcheck?uid=${this.member.userid}`;
const headers = {"Content-Type":"application/json"};
const response = await this.axios.get(url, {headers:headers});
console.log(response);
if(response.data.result === 1){
alert('중복된 아이디가 존재합니다.')
this.$refs.id.focus();
return false;
}
if(response.data.result === 0){
alert('사용가능한 아이디입니다')
this.$refs.pw.focus();
}
}