Jeongmin Seo

Update login

"use strict";
//모듈
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
//라우팅
const home = require("./src/routes/home");
......@@ -8,6 +9,9 @@ const home = require("./src/routes/home");
app.set("views", "./src/views");
app.set("view engine", "ejs");
app.use(express.static(`${__dirname}/src/public`));
app.use(bodyParser.json());
//url통해 전달되는 데이터에 한글, 공백 등의 문자 오류 해결
app.use(bodyParser.urlencoded({extended: true}));
app.use("/", home); //미들웨어 등록해주는 method
......
{
"requires": true,
"name": "login",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"accepts": {
"version": "1.3.8",
......
......@@ -6,12 +6,13 @@
"login": "www.js"
},
"dependencies": {
"body-parser": "^1.20.0",
"ejs": "^3.1.8",
"express": "^4.18.1"
},
"devDependencies": {},
"scripts": {
"start": "node ./bin/www.js",
"start": "nodemon ./bin/www.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
......
'use strict';
class UserStorage {
//더미데이터
// # = private 변수로 은닉화
static #users = {
id: ["jeongmin", "jumi"],
password: ["1234", "1234"],
name: ["정민", "주미"],
};
static getUsers(...fields) {
const users = this.#users;
const newUsers = fields.reduce((newUsers, field) => {
if (users.hasOwnProperty(field)) {
newUsers[field] = users[field];
}
return newUsers;
}, {});
return newUsers;
}
}
module.exports = UserStorage;
\ No newline at end of file
'use strict';
console.log("hello");
\ No newline at end of file
const id = document.querySelector("#id"),
password = document.querySelector("#password"),
loginBtn = document.querySelector("button");
loginBtn.addEventListener("click", login);
function login() {
const req = {
id : id.value,
password : password.value,
};
fetch("/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(req),
})
.then((res) => res.json())
.then((res) => {
if (res.success) {
//성공하면 이동
location.href = "/";
} else {
alert(res.msg);
}
})
.catch((err) => {
console.error("로그인 중 에러 발생");
});
}
\ No newline at end of file
......
"use strict";
const hello = (req, res) => {
res.render("home/index");
const UserStorage = require("../../models/UserStorage");
const output = {
hello: (req, res) => {
res.render("home/index");
},
login: (req, res) => {
res.render("home/login");
},
};
const login = (req, res) => {
res.render("home/login");
const process = {
login: (req, res) => {
const id = req.body.id,
password = req.body.password;
const users = UserStorage.getUsers("id", "password");
// console.log(UserStorage.getUsers("id", "password","name"));
const response = {};
if (users.id.includes(id)) {
const idx = users.id.indexOf(id);
if (users.password[idx] === password) {
response.success = true;
return res.json(response);
}
}
response.success = false;
response.msg = "로그인에 실패하였습니다."
return res.json(response);
},
};
module.exports = {
hello,
login,
output,
process,
};
\ No newline at end of file
......
......@@ -5,7 +5,8 @@ const router = express.Router();
const ctrl = require("./home.ctrl");
router.get("/", ctrl.hello);
router.get("/login", ctrl.login);
router.get("/", ctrl.output.hello);
router.get("/login", ctrl.output.login);
router.post("/login", ctrl.process.login);
module.exports = router;
\ No newline at end of file
......
......@@ -4,12 +4,12 @@
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="/js/home/login.js"></script>
<script src="/js/home/login.js" defer></script>
<title>Document</title>
</head>
<body>
<input type="text" placeholder="아이디" /><br />
<input type="text" placeholder="비밀번호" /><br />
<input id="id" type="text" placeholder="아이디" /><br />
<input id="password" type="text" placeholder="비밀번호" /><br />
<button>로그인</button>
</body>
</html>
......