swa07016

중복 가입 방지 구현

...@@ -36,7 +36,6 @@ const SigninPage = (props) => { ...@@ -36,7 +36,6 @@ const SigninPage = (props) => {
36 cookie.save('username', response.username, { 36 cookie.save('username', response.username, {
37 expires 37 expires
38 }) 38 })
39 - alert('Login success');
40 props.history.push('/mypick'); 39 props.history.push('/mypick');
41 } else if(response.message === "user does not exist"){ 40 } else if(response.message === "user does not exist"){
42 alert('User does not exist'); 41 alert('User does not exist');
......
...@@ -37,12 +37,18 @@ const SigninPage = (props) => { ...@@ -37,12 +37,18 @@ const SigninPage = (props) => {
37 fetch("http://localhost:3000/api/signup", signup_info) 37 fetch("http://localhost:3000/api/signup", signup_info)
38 .then(response => response.json()) 38 .then(response => response.json())
39 .then(json => { 39 .then(json => {
40 - if(json.code === 200) { 40 + if(json.message === 'success') {
41 alert('회원가입에 성공했습니다.'); 41 alert('회원가입에 성공했습니다.');
42 props.history.push('/signin'); 42 props.history.push('/signin');
43 } 43 }
44 - else if(json.code === 400) { 44 + else if(json.message === 'user exist') {
45 + alert('이미 존재하는 유저입니다');
46 + setUsername('');
47 + setPassword('');
48 + } else {
45 alert('회원가입에 실패했습니다.'); 49 alert('회원가입에 실패했습니다.');
50 + setUsername('');
51 + setPassword('');
46 } 52 }
47 }) 53 })
48 } 54 }
......
...@@ -40,9 +40,19 @@ app.get("/api/datas", (req, res) => { ...@@ -40,9 +40,19 @@ app.get("/api/datas", (req, res) => {
40 res.send(iconv.decode(dataBuffer, "EUC-KR").toString()); 40 res.send(iconv.decode(dataBuffer, "EUC-KR").toString());
41 }); 41 });
42 42
43 -// ???? ???? ??
44 // signup 43 // signup
45 app.post("/api/signup", (req, res) => { 44 app.post("/api/signup", (req, res) => {
45 +
46 + let sql_usercheck = `SELECT * FROM USER WHERE name='${req.body.username}';`;
47 + connection.query(sql_usercheck, (err, rows, fields) => {
48 + console.log(rows);
49 + if(rows.length!==0) {
50 + return res.json({
51 + code: 400,
52 + message: 'user exist'
53 + })
54 + }
55 + else {
46 let sql = "INSERT INTO USER (name, pw) VALUES(?, ?)"; 56 let sql = "INSERT INTO USER (name, pw) VALUES(?, ?)";
47 let plainPassword = req.body.password; 57 let plainPassword = req.body.password;
48 bcrypt.hash(plainPassword, saltRounds, function (err, hash) { 58 bcrypt.hash(plainPassword, saltRounds, function (err, hash) {
...@@ -62,6 +72,8 @@ app.post("/api/signup", (req, res) => { ...@@ -62,6 +72,8 @@ app.post("/api/signup", (req, res) => {
62 } 72 }
63 }); 73 });
64 }); 74 });
75 + }
76 + })
65 }); 77 });
66 78
67 app.post("/api/signin", (req, res) => { 79 app.post("/api/signin", (req, res) => {
......