user.js
1.93 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var express = require('express');
var request = require('request');
var async = require('async');
var router = express.Router();
var CryptoJS = require("crypto-js");
router.post('/update', function(req, res, next) {
var user_id=req.body.user_id;
var user_pw=req.body.user_pw;
var user_pw2=req.body.user_pw2;
var user_email=req.body.user_email;
var user_phone=req.body.user_phone;
if(user_pw===user_pw2) {
user_pw=cryptoM.encrypt(user_pw);
var sql='UPDATE users SET user_pw = ?, user_email= ?,user_phone = ? WHERE user_id = ?';
var values=[user_pw, user_email,user_phone, user_id];
connection.query(sql, values, function (err) {
if (err) {
console.log("updating user failed");
next(err);
} else {
res.status(200).send('update');
}
});
}
else
{
res.status(404).send(err);
}
});
router.post('/delete', function(req, res, next) {
var user_id=req.body.user_id;
var user_pw=req.body.user_pw;
var user_pw_temp =cryptoM.encrypt(user_pw); //받아온 비밀번호를 암호화화여 임시변수에 저장
var sqlquery = "SELECT user_pw FROM users WHERE user_id = ?"; // user_id의 비밀번호를 가져옴
connection.query(sqlquery, [user_id], function (err, rows) {
if (rows[0].user_pw == user_pw_temp) { // 받아온 비밀번호와 user_id의 비밀번호가 같은지 대조
var sql = 'delete from users where user_id = ?'; // 만약 같다면 삭제 진행
var values = [user_id];
connection.query(sql, values, function (err) {
if (err) {
console.log("delete user failed");
throw err;
} else {
res.status(200).send('delete');
}
});
} else {
res.status(404).send(err);
}
});
});
module.exports = router;