김대휘

bcrypt 암호화

Showing 1 changed file with 55 additions and 7 deletions
...@@ -5,9 +5,10 @@ const app = express(); ...@@ -5,9 +5,10 @@ const app = express();
5 5
6 const port = process.env.PORT || 5000; 6 const port = process.env.PORT || 5000;
7 7
8 -const bcrypt = require('bcrypt'); 8 +const bcrypt = require("bcrypt");
9 const saltRounds = 10; 9 const saltRounds = 10;
10 10
11 +
11 const data = fs.readFileSync("./database.json"); 12 const data = fs.readFileSync("./database.json");
12 const conf = JSON.parse(data); 13 const conf = JSON.parse(data);
13 const mysql = require("mysql"); 14 const mysql = require("mysql");
...@@ -33,8 +34,18 @@ app.get("/api/cards", (req, res) => { ...@@ -33,8 +34,18 @@ app.get("/api/cards", (req, res) => {
33 34
34 app.post("/api/addcard", (req, res) => { 35 app.post("/api/addcard", (req, res) => {
35 const data = req.body; 36 const data = req.body;
36 - const sql = "INSERT INTO CARDINFO(isPublic,name,date,time,title,todo,ck) VALUES(?,?,?,?,?,?,?);"; 37 + const sql =
37 - const params =[data.isPublic,data.name,data.date,data.time,data.title,data.todo,data.ck]; 38 + "INSERT INTO CARDINFO(isPublic,name,date,time,title,todo,ck) VALUES(?,?,?,?,?,?,?);";
39 +
40 + const params = [
41 + data.isPublic,
42 + data.name,
43 + data.date,
44 + data.time,
45 + data.title,
46 + data.todo,
47 + data.ck,
48 + ];
38 connection.query(sql, params, (err, rows, fields) => { 49 connection.query(sql, params, (err, rows, fields) => {
39 if (err) { 50 if (err) {
40 res.send({ 51 res.send({
...@@ -50,12 +61,12 @@ app.post("/api/addcard", (req, res) => { ...@@ -50,12 +61,12 @@ app.post("/api/addcard", (req, res) => {
50 }); 61 });
51 }); 62 });
52 63
53 -app.post("/api/signup", (req, res) => { 64 +app.post("/api/signup", async (req, res) => {
54 const data = req.body; 65 const data = req.body;
55 -
56 - console.log(data);
57 const sql = "INSERT INTO USERINFO(userID,userPW,userName) VALUES(?,?,?);"; 66 const sql = "INSERT INTO USERINFO(userID,userPW,userName) VALUES(?,?,?);";
58 - const params =[data.userID,data.userPW,data.userName]; 67 +
68 + await bcrypt.hash(data.userPW, saltRounds, function (err, hash) {
69 + let params = [data.userID, hash, data.userName];
59 connection.query(sql, params, (err, rows, fields) => { 70 connection.query(sql, params, (err, rows, fields) => {
60 if (err) { 71 if (err) {
61 res.send({ 72 res.send({
...@@ -69,6 +80,43 @@ app.post("/api/signup", (req, res) => { ...@@ -69,6 +80,43 @@ app.post("/api/signup", (req, res) => {
69 }); 80 });
70 } 81 }
71 }); 82 });
83 + });
72 }); 84 });
73 85
86 +app.post("/api/login", (req, res) => {
87 + const data = req.body;
88 + const enteredID = data.userID;
89 + const enteredPW = data.userPW;
90 + connection.query('SELECT * FROM USERINFO WHERE userID = ?', [enteredID],
91 + function( error, results, fields) {
92 + if (error) {
93 + // console.log("error ocurred", error);
94 + res.send({
95 + "code": 400,
96 + "failed": "error ocurred"
97 + })
98 + } else {
99 + // console.log('The solution is: ', results);
100 + if(results.length > 0) {
101 + if(results[0].userID == password) {
102 + res.send({
103 + "code": 200,
104 + "success": "login sucessfull"
105 + });
106 + } else {
107 + res.send({
108 + "code": 204,
109 + "success": "Email and password does not match"
110 + });
111 + }
112 + } else {
113 + res.send({
114 + "code":204,
115 + "success": "Email does not exists"
116 + });
117 + }
118 + }
119 + })
120 +}
121 +
74 app.listen(port, () => console.log(`Listening on port ${port}`)); 122 app.listen(port, () => console.log(`Listening on port ${port}`));
......