swa07016

'/api/signin' 백엔드 구현

...@@ -3,4 +3,5 @@ json_datas.json ...@@ -3,4 +3,5 @@ json_datas.json
3 client/public/images 3 client/public/images
4 client/node_modules 4 client/node_modules
5 client/src/config 5 client/src/config
6 -database.json
...\ No newline at end of file ...\ No newline at end of file
6 +database.json
7 +jwt_key.json
...\ No newline at end of file ...\ No newline at end of file
......
1 -import React from 'react'; 1 +import React, { useState } from 'react';
2 import { Button, Form, FormGroup, Label, Input} from 'reactstrap'; 2 import { Button, Form, FormGroup, Label, Input} from 'reactstrap';
3 -
4 import {FacebookLoginButton} from 'react-social-login-buttons'; 3 import {FacebookLoginButton} from 'react-social-login-buttons';
5 4
5 +
6 +
6 const SigninPage = (props) => { 7 const SigninPage = (props) => {
8 +
9 +// const [userName, setUserName] = useState('');
10 +// const [userPw, setuserPw] = useState('');
11 +
12 +// const signinApi = (user) => {
13 +// return fetch('/api/signin', {
14 +// method: 'POST',
15 +// headers: {
16 +// 'Content-Type': 'application/json'
17 +// },
18 +// body: JSON.stringify(user)
19 +// }).then(response => response.json())
20 +// }
21 +
22 +// const handleSubmit = async (e) => {
23 +// e.preventDefault();
24 +// if (!userId || !userPw) {
25 +// return;
26 +// }
27 +// try {
28 +// const response = await loginApi({
29 +// user_id: userId,
30 +// user_pw: userPw
31 +// });
32 +
33 +// if (response.result === 'ok') {
34 +// setToken();
35 +// } else {
36 +// throw new Error(response.error);
37 +// }
38 +// } catch (err) {
39 +// alert('로그인에 실패했습니다.');
40 +// setUserId('');
41 +// setUserPw('');
42 +// console.error('login error', err);
43 +// }
44 +// };
45 +// };
7 return ( 46 return (
8 <> 47 <>
9 <Form style={{ 48 <Form style={{
......
...@@ -105,6 +105,11 @@ ...@@ -105,6 +105,11 @@
105 "concat-map": "0.0.1" 105 "concat-map": "0.0.1"
106 } 106 }
107 }, 107 },
108 + "buffer-equal-constant-time": {
109 + "version": "1.0.1",
110 + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz",
111 + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk="
112 + },
108 "bytes": { 113 "bytes": {
109 "version": "3.1.0", 114 "version": "3.1.0",
110 "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", 115 "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz",
...@@ -265,6 +270,14 @@ ...@@ -265,6 +270,14 @@
265 "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", 270 "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz",
266 "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=" 271 "integrity": "sha1-+hN8S9aY7fVc1c0CrFWfkaTEups="
267 }, 272 },
273 + "ecdsa-sig-formatter": {
274 + "version": "1.0.11",
275 + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz",
276 + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==",
277 + "requires": {
278 + "safe-buffer": "^5.0.1"
279 + }
280 + },
268 "ee-first": { 281 "ee-first": {
269 "version": "1.1.1", 282 "version": "1.1.1",
270 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 283 "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
...@@ -545,6 +558,49 @@ ...@@ -545,6 +558,49 @@
545 "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz", 558 "resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz",
546 "integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw==" 559 "integrity": "sha512-mrqyZKfX5EhL7hvqcV6WG1yYjnjeuYDzDhhcAAUrq8Po85NBQBJP+ZDUT75qZQ98IkUoBqdkExkukOU7Ts2wrw=="
547 }, 560 },
561 + "jsonwebtoken": {
562 + "version": "8.5.1",
563 + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.5.1.tgz",
564 + "integrity": "sha512-XjwVfRS6jTMsqYs0EsuJ4LGxXV14zQybNd4L2r0UvbVnSF9Af8x7p5MzbJ90Ioz/9TI41/hTCvznF/loiSzn8w==",
565 + "requires": {
566 + "jws": "^3.2.2",
567 + "lodash.includes": "^4.3.0",
568 + "lodash.isboolean": "^3.0.3",
569 + "lodash.isinteger": "^4.0.4",
570 + "lodash.isnumber": "^3.0.3",
571 + "lodash.isplainobject": "^4.0.6",
572 + "lodash.isstring": "^4.0.1",
573 + "lodash.once": "^4.0.0",
574 + "ms": "^2.1.1",
575 + "semver": "^5.6.0"
576 + },
577 + "dependencies": {
578 + "ms": {
579 + "version": "2.1.2",
580 + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
581 + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
582 + }
583 + }
584 + },
585 + "jwa": {
586 + "version": "1.4.1",
587 + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz",
588 + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==",
589 + "requires": {
590 + "buffer-equal-constant-time": "1.0.1",
591 + "ecdsa-sig-formatter": "1.0.11",
592 + "safe-buffer": "^5.0.1"
593 + }
594 + },
595 + "jws": {
596 + "version": "3.2.2",
597 + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz",
598 + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==",
599 + "requires": {
600 + "jwa": "^1.4.1",
601 + "safe-buffer": "^5.0.1"
602 + }
603 + },
548 "locate-path": { 604 "locate-path": {
549 "version": "3.0.0", 605 "version": "3.0.0",
550 "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz", 606 "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-3.0.0.tgz",
...@@ -559,6 +615,41 @@ ...@@ -559,6 +615,41 @@
559 "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", 615 "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
560 "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" 616 "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
561 }, 617 },
618 + "lodash.includes": {
619 + "version": "4.3.0",
620 + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz",
621 + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8="
622 + },
623 + "lodash.isboolean": {
624 + "version": "3.0.3",
625 + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz",
626 + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY="
627 + },
628 + "lodash.isinteger": {
629 + "version": "4.0.4",
630 + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz",
631 + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M="
632 + },
633 + "lodash.isnumber": {
634 + "version": "3.0.3",
635 + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz",
636 + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w="
637 + },
638 + "lodash.isplainobject": {
639 + "version": "4.0.6",
640 + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz",
641 + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs="
642 + },
643 + "lodash.isstring": {
644 + "version": "4.0.1",
645 + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz",
646 + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE="
647 + },
648 + "lodash.once": {
649 + "version": "4.1.1",
650 + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz",
651 + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w="
652 + },
562 "media-typer": { 653 "media-typer": {
563 "version": "0.3.0", 654 "version": "0.3.0",
564 "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 655 "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
20 "concurrently": "^5.2.0", 20 "concurrently": "^5.2.0",
21 "express": "^4.17.1", 21 "express": "^4.17.1",
22 "iconv-lite": "^0.5.1", 22 "iconv-lite": "^0.5.1",
23 + "jsonwebtoken": "^8.5.1",
23 "mysql": "^2.18.1" 24 "mysql": "^2.18.1"
24 } 25 }
25 } 26 }
......
1 -const express = require('express'); 1 +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 -const iconv = require('iconv-lite'); 5 +const iconv = require("iconv-lite");
6 +const jwt = require("jsonwebtoken");
6 7
7 -const bcrypt = require('bcrypt'); 8 +const bcrypt = require("bcrypt");
8 -const saltRounds = 10; 9 +const saltRounds = 10;
9 10
10 const app = express(); 11 const app = express();
11 const port = process.env.PORT || 5000; 12 const port = process.env.PORT || 5000;
12 13
13 -const dataBuffer = fs.readFileSync('json_datas.json') 14 +const dataBuffer = fs.readFileSync("json_datas.json");
14 15
16 +const jwt_key = fs.readFileSync("./jwt_key.json");
17 +const jwt_secret_key = JSON.parse(jwt_key);
15 18
16 // db connection 19 // db connection
17 -const data = fs.readFileSync('./database.json'); 20 +const data = fs.readFileSync("./database.json");
18 const conf = JSON.parse(data); 21 const conf = JSON.parse(data);
19 22
20 const connection = mysql.createConnection({ 23 const connection = mysql.createConnection({
21 - host: conf.host, 24 + host: conf.host,
22 - user: conf.user, 25 + user: conf.user,
23 - password: conf.password, 26 + password: conf.password,
24 - port: conf.port, 27 + port: conf.port,
25 - database: conf.database 28 + database: conf.database,
26 }); 29 });
27 30
28 connection.connect(); 31 connection.connect();
...@@ -30,40 +33,129 @@ connection.connect(); ...@@ -30,40 +33,129 @@ connection.connect();
30 app.use(bodyParser.json()); 33 app.use(bodyParser.json());
31 app.use(bodyParser.urlencoded({ extended: true })); 34 app.use(bodyParser.urlencoded({ extended: true }));
32 35
33 -app.get('/api/hello', (req, res) => { 36 +app.get("/api/hello", (req, res) => {
34 - res.send('Hello skrrrr!'); 37 + res.send("Hello skrrrr!");
35 }); 38 });
36 39
37 // datas 전달 40 // datas 전달
38 -app.get('/api/datas', (req, res) => { 41 +app.get("/api/datas", (req, res) => {
39 - iconv.extendNodeEncodings(); 42 + iconv.extendNodeEncodings();
40 - res.header("Access-Control-Allow-Origin", "*"); 43 + res.header("Access-Control-Allow-Origin", "*");
41 - res.send(iconv.decode(dataBuffer, 'EUC-KR').toString()); 44 + res.send(iconv.decode(dataBuffer, "EUC-KR").toString());
42 -}) 45 +});
43 46
47 +// ???? ???? ??
44 // signup 48 // signup
45 -app.post('/api/signup', (req, res) => { 49 +app.post("/api/signup", (req, res) => {
46 - let sql = 'INSERT INTO USER (name, pw) VALUES(?, ?)'; 50 + let sql = "INSERT INTO USER (name, pw) VALUES(?, ?)";
47 - let plainPassword = req.body.password; 51 + let plainPassword = req.body.password;
48 - bcrypt.hash(plainPassword, saltRounds, function(err, hash) { 52 + bcrypt.hash(plainPassword, saltRounds, function (err, hash) {
53 + const params = [req.body.username, hash];
54 + connection.query(sql, params, (err, rows, fields) => {
55 + if (err) {
56 + console.log(err);
57 + res.send({
58 + code: 400,
59 + message: "error",
60 + });
61 + } else {
62 + res.send({
63 + code: 200,
64 + message: "success",
65 + });
66 + }
67 + });
68 + });
69 +});
70 +
71 +// ????
72 +// res.send({
73 +// "code":200,
74 +// "message": "success"
75 +// })
76 +// ????
77 +// jwt_secret_key.value
78 +// signin
79 +app.post("/api/signin", (req, res) => {
80 + // ????
81 +// res.send('aa');
82 + const name = req.body.username;
83 + let sql = `SELECT name, pw FROM USER WHERE name='${req.body.username}';`;
84 +
85 + connection.query(sql, (err, rows, fields) => {
86 +
87 + if (!rows) {
88 + res.send({
89 + code: 400,
90 + message: "failed",
91 + });
92 + return ;
93 + }
49 94
50 - const params = [req.body.username, hash]; 95 + else{
51 - connection.query(sql, params, (err, rows, fields) => { 96 +
52 - if(err){ 97 + bcrypt.compare(req.body.password, rows[0].pw, function (err, result){
53 - console.log(err); 98 + const pw = rows[0].pw;
54 - res.send({ 99 + if(result) {
55 - "code":400, 100 +
56 - "message": "error" 101 + try {
57 - }) 102 + // jwt.sign() ???: ?? ??
58 - } 103 + const token = jwt.sign(
59 - else { 104 + {
60 - res.send({ 105 + name,
61 - "code":200, 106 + pw,
62 - "message": "success" 107 + },
63 - }) 108 + jwt_secret_key.value,
64 - } 109 + {
65 - }) 110 + expiresIn: "60m", // 60?
111 + issuer: "admin",
112 + }
113 + );
114 +
115 + return res.json({
116 + code: 200,
117 + message: '??? ???????.',
118 + token,
119 + });
120 +
121 + } catch (error) {
122 + console.error(error);
123 + return res.status(500).json({
124 + code: 500,
125 + message: '?? ??',
126 + });
127 + }
128 +
129 + } else {
130 + res.send({
131 + code: 400,
132 + message: "failed",
133 + });
134 + }
135 + })
136 + }
66 }) 137 })
67 -}) 138 +});
139 +// else {
140 +// bcrypt.compare(req.body.password, rows[0].pw, function (err, res) {
141 +// console.log(res);
142 +// if(!res) {
143 +// res.send({
144 +// code: 400,
145 +// message: "failed",
146 +// });
147 +// }
148 +// else {
149 +// // ???? ??? ?
150 +// const pw = rows[0].pw;
151 +
152 +// }
153 +// });
154 +
155 +// }
156 +
157 +// });
158 +
159 +
68 160
69 -app.listen(port, () => console.log(`Listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file
161 +app.listen(port, () => console.log(`Listening on port ${port}`));
......