공태현

Implement login page with MongoDB

......@@ -68,11 +68,12 @@ app.post('/api/users/register', (req,res) => {
// 로그인 .
app.post('/api/users/login', (req ,res) => {
console.log(req.body)
User.findOne({name : req.body.name}, (err, user) => {
if (!user) {
return res.json({
loginSuccess: false,
message : "이름이 일치하는 사용자가 없습니다 !"
message : "이름이 일치하는 사용자가 없습니다 !",
})
}
else if (req.body.password === user.password) {
......
......@@ -5,6 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Main Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
Main Page
......@@ -13,5 +15,12 @@
</div>
<script type="text/javascript" src="./main.js"></script>
<div>
<input id = "name" type = "text" placeholder="이름">
<input id = "password" type = "text" placeholder="비밀번호">
<button type="submit" id = "button">
로그인
</button>
</div>
</body>
</html>
\ No newline at end of file
......
......@@ -7,4 +7,36 @@ function moveSquartPage()
const movePage = document.getElementById("moveSquart")
movePage.addEventListener('click', moveSquartPage);
\ No newline at end of file
movePage.addEventListener('click', moveSquartPage);
$(document).ready(function(){
$('#button').click(function(){
let sendName = $('#name').val();
let sendPassword = $('#password').val();
let sendData = {
"name" : sendName,
"password" : sendPassword
}
$.ajax({
contentType : "application/json; charset=utf-8",
type : 'post',
url : 'api/users/login',
data : JSON.stringify(sendData),
dataType : 'JSON',
success : function(datas) {
if (datas.loginSuccess)
{
moveSquartPage()
}
else
{
alert(datas.message)
}
}
})
})
})
\ No newline at end of file
......