swa07016

bcrypt를 이용한 패스워드 암호화

This diff is collapsed. Click to expand it.
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
15 "author": "SeongHoon", 15 "author": "SeongHoon",
16 "license": "ISC", 16 "license": "ISC",
17 "dependencies": { 17 "dependencies": {
18 + "bcrypt": "^4.0.1",
18 "body-parser": "^1.19.0", 19 "body-parser": "^1.19.0",
19 "concurrently": "^5.2.0", 20 "concurrently": "^5.2.0",
20 "express": "^4.17.1", 21 "express": "^4.17.1",
......
...@@ -2,7 +2,10 @@ const express = require('express'); ...@@ -2,7 +2,10 @@ const express = require('express');
2 const bodyParser = require('body-parser'); 2 const bodyParser = require('body-parser');
3 const fs = require('fs'); 3 const fs = require('fs');
4 const mysql = require('mysql'); 4 const mysql = require('mysql');
5 -var iconv = require('iconv-lite'); 5 +const iconv = require('iconv-lite');
6 +
7 +const bcrypt = require('bcrypt');
8 +const saltRounds = 10;
6 9
7 const app = express(); 10 const app = express();
8 const port = process.env.PORT || 5000; 11 const port = process.env.PORT || 5000;
...@@ -41,23 +44,26 @@ app.get('/api/datas', (req, res) => { ...@@ -41,23 +44,26 @@ app.get('/api/datas', (req, res) => {
41 // signup 44 // signup
42 app.post('/api/signup', (req, res) => { 45 app.post('/api/signup', (req, res) => {
43 let sql = 'INSERT INTO USER (name, pw) VALUES(?, ?)'; 46 let sql = 'INSERT INTO USER (name, pw) VALUES(?, ?)';
44 - const params = [req.body.username, req.body.password]; 47 + let plainPassword = req.body.password;
45 - connection.query(sql, params, (err, rows, fields) => { 48 + bcrypt.hash(plainPassword, saltRounds, function(err, hash) {
46 - if(err){ 49 +
47 - console.log(err); 50 + const params = [req.body.username, hash];
48 - res.send({ 51 + connection.query(sql, params, (err, rows, fields) => {
49 - "code":400, 52 + if(err){
50 - "message": "error" 53 + console.log(err);
51 - }) 54 + res.send({
52 - } 55 + "code":400,
53 - else { 56 + "message": "error"
54 - res.send({ 57 + })
55 - "code":200, 58 + }
56 - "message": "success" 59 + else {
57 - }) 60 + res.send({
58 - } 61 + "code":200,
62 + "message": "success"
63 + })
64 + }
65 + })
59 }) 66 })
60 -
61 }) 67 })
62 68
63 app.listen(port, () => console.log(`Listening on port ${port}`)); 69 app.listen(port, () => console.log(`Listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file
......