김대휘

Login Check

......@@ -54,12 +54,37 @@ const useStyles = makeStyles((theme) => ({
},
}));
export default function LandingPage() {
export default function LandingPage(props) {
const classes = useStyles();
const [userID, setUserID] = useState();
const [userPW, setUserPW] = useState();
const loginApi = (data) => {
return fetch("/api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}).then((response) => response.json());
};
const handleLogin = () => {
if (!userID || !userPW) {
alert("All blanks must be filled. Try agian.");
}
else {
loginApi({
userID: userID,
userPW: userPW,
});
alert("Successfully login!");
props.history.push("/login");
}
};
return (
<div className={classes.root}>
<Paper className={classes.paper} elevation={3}>
......@@ -85,7 +110,7 @@ export default function LandingPage() {
setUserPW(e.target.value);
}}
/>
<Button className={classes.signin} variant="outlined" size="small">
<Button className={classes.signin} variant="outlined" size="small" onClick={handleLogin}>
Login
</Button>
<div className={classes.signup}>
......
......@@ -8,7 +8,6 @@ const port = process.env.PORT || 5000;
const bcrypt = require("bcrypt");
const saltRounds = 10;
const data = fs.readFileSync("./database.json");
const conf = JSON.parse(data);
const mysql = require("mysql");
......@@ -98,7 +97,10 @@ app.post("/api/login", (req, res) => {
} else {
// console.log('The solution is: ', results);
if(results.length > 0) {
if(results[0].userID == password) {
bcrypt.compare(enteredPW, results[0].userPW, function(err, check) {
console.log(check);
if(check) {
console.log("sec")
res.send({
"code": 200,
"success": "login sucessfull"
......@@ -106,9 +108,10 @@ app.post("/api/login", (req, res) => {
} else {
res.send({
"code": 204,
"success": "Email and password does not match"
"success": "id and password does not match."
});
}
});
} else {
res.send({
"code":204,
......@@ -117,6 +120,5 @@ app.post("/api/login", (req, res) => {
}
}
})
}
app.listen(port, () => console.log(`Listening on port ${port}`));
});
app.listen(port, () => console.log(`Listening on port ${port}`));
\ No newline at end of file
......