김대휘

Login Check

...@@ -54,12 +54,37 @@ const useStyles = makeStyles((theme) => ({ ...@@ -54,12 +54,37 @@ const useStyles = makeStyles((theme) => ({
54 }, 54 },
55 })); 55 }));
56 56
57 -export default function LandingPage() { 57 +
58 +export default function LandingPage(props) {
58 const classes = useStyles(); 59 const classes = useStyles();
59 60
60 const [userID, setUserID] = useState(); 61 const [userID, setUserID] = useState();
61 const [userPW, setUserPW] = useState(); 62 const [userPW, setUserPW] = useState();
62 63
64 + const loginApi = (data) => {
65 + return fetch("/api/login", {
66 + method: "POST",
67 + headers: {
68 + "Content-Type": "application/json",
69 + },
70 + body: JSON.stringify(data),
71 + }).then((response) => response.json());
72 + };
73 +
74 + const handleLogin = () => {
75 + if (!userID || !userPW) {
76 + alert("All blanks must be filled. Try agian.");
77 + }
78 + else {
79 + loginApi({
80 + userID: userID,
81 + userPW: userPW,
82 + });
83 + alert("Successfully login!");
84 + props.history.push("/login");
85 + }
86 + };
87 +
63 return ( 88 return (
64 <div className={classes.root}> 89 <div className={classes.root}>
65 <Paper className={classes.paper} elevation={3}> 90 <Paper className={classes.paper} elevation={3}>
...@@ -85,7 +110,7 @@ export default function LandingPage() { ...@@ -85,7 +110,7 @@ export default function LandingPage() {
85 setUserPW(e.target.value); 110 setUserPW(e.target.value);
86 }} 111 }}
87 /> 112 />
88 - <Button className={classes.signin} variant="outlined" size="small"> 113 + <Button className={classes.signin} variant="outlined" size="small" onClick={handleLogin}>
89 Login 114 Login
90 </Button> 115 </Button>
91 <div className={classes.signup}> 116 <div className={classes.signup}>
......
...@@ -8,7 +8,6 @@ const port = process.env.PORT || 5000; ...@@ -8,7 +8,6 @@ const port = process.env.PORT || 5000;
8 const bcrypt = require("bcrypt"); 8 const bcrypt = require("bcrypt");
9 const saltRounds = 10; 9 const saltRounds = 10;
10 10
11 -
12 const data = fs.readFileSync("./database.json"); 11 const data = fs.readFileSync("./database.json");
13 const conf = JSON.parse(data); 12 const conf = JSON.parse(data);
14 const mysql = require("mysql"); 13 const mysql = require("mysql");
...@@ -98,7 +97,10 @@ app.post("/api/login", (req, res) => { ...@@ -98,7 +97,10 @@ app.post("/api/login", (req, res) => {
98 } else { 97 } else {
99 // console.log('The solution is: ', results); 98 // console.log('The solution is: ', results);
100 if(results.length > 0) { 99 if(results.length > 0) {
101 - if(results[0].userID == password) { 100 + bcrypt.compare(enteredPW, results[0].userPW, function(err, check) {
101 + console.log(check);
102 + if(check) {
103 + console.log("sec")
102 res.send({ 104 res.send({
103 "code": 200, 105 "code": 200,
104 "success": "login sucessfull" 106 "success": "login sucessfull"
...@@ -106,9 +108,10 @@ app.post("/api/login", (req, res) => { ...@@ -106,9 +108,10 @@ app.post("/api/login", (req, res) => {
106 } else { 108 } else {
107 res.send({ 109 res.send({
108 "code": 204, 110 "code": 204,
109 - "success": "Email and password does not match" 111 + "success": "id and password does not match."
110 }); 112 });
111 } 113 }
114 + });
112 } else { 115 } else {
113 res.send({ 116 res.send({
114 "code":204, 117 "code":204,
...@@ -117,6 +120,5 @@ app.post("/api/login", (req, res) => { ...@@ -117,6 +120,5 @@ app.post("/api/login", (req, res) => {
117 } 120 }
118 } 121 }
119 }) 122 })
120 -} 123 +});
121 -
122 app.listen(port, () => console.log(`Listening on port ${port}`)); 124 app.listen(port, () => console.log(`Listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file
......