최정민

FEAT : MYSQL DB와 연동하여 로그인

-
This diff is collapsed. Click to expand it.
......@@ -6,6 +6,7 @@
"start": "nodemon ./bin/www"
},
"dependencies": {
"body-parser": "^1.19.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "^3.1.6",
......@@ -14,6 +15,7 @@
"google-auth-library": "^7.0.4",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"mysql": "^2.18.1",
"nodemon": "^2.0.7",
"session-file-store": "^1.5.0"
}
......
var express = require("express");
var router = express.Router();
var bodyParser=require('body-parser');
var { OAuth2Client } = require("google-auth-library");
var CLIENT_ID =
"94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com";
var client = new OAuth2Client(CLIENT_ID);
var mysql=require('mysql')
var session = require("express-session");
var FileStore = require("session-file-store")(session);
router.use(bodyParser.urlencoded({extended:false})); //url인코딩 x
router.use(bodyParser.json()); //json방식으로 파
router.use(
session({
secret: "209", // 암호화
......@@ -14,7 +18,13 @@ router.use(
store: new FileStore(),
})
);
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'g79465',
database : 'caferecommend'
});
connection.connect();
/* GET home page. */
router.get("/", function (req, res, next) {
res.render("index", {
......@@ -44,16 +54,60 @@ router.post("/index", (req, res) => {
});
router.get("/login", checkAuthenticated, (req, res) => {
let user = req.user;
req.session.user = user;
res.render("login", { user: req.session.user });
var sql = 'SELECT * FROM USER WHERE EMAIL=?';
var parameter=[req.session.user.email];
connection.query(sql,parameter, function(err, row){
if(err){
console.log(err);
}
else {
if (row.length > 0) {
console.log("이미 가입이 되어있는 아이디")
req.session.user.nickname = row[0].NICKNAME;
req.session.user.age = row[0].AGE;
req.session.user.gender = row[0].GENDER;
return res.render('map', {user : req.session.user})
}
else{
return res.render("login", { user: req.session.user, message:'none' });
}
}
});
});
router.post("/login", (req, res) => {
console.log(req.body.nickname);
console.log(req.body.age);
console.log(req.body.gender);
return res.render("login", { user: req.session.user });
var sql =' SELECT * FROM USER WHERE NICKNAME=?';
var parameter=[req.body.nickname];
connection.query(sql, parameter, function(err,row){
if(err){
console.log(err);
}
if(row.length>0){
console.log('동일 닉네임있음');
return res.render("login", {user : req.session.user, message: 'same nickname'});
}
else{
req.session.user.nickname=req.body.nickname;
req.session.user.age=req.body.age;
req.session.user.gender=req.body.gender;
var sql='INSERT INTO USER(EMAIL, NAME, NICKNAME, AGE, GENDER) VALUES(?,?,?,?,?)';
var parameter=[req.session.user.email, req.session.user.name, req.session.user.nickname, req.session.user.age, req.session.user.gender];
connection.query(sql, parameter, function(err, row){
if(err){
console.log(err);
}
else{
console.log("새로운 user데이터 입력");
}
});
return res.render("map", { user : req.session.user });
}
});
});
module.exports = router;
......@@ -69,12 +123,11 @@ function checkAuthenticated(req, res, next) {
const payload = ticket.getPayload();
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
console.log(user.name);
}
verify()
.then(() => {
req.user = user;
req.session.user.name = user.name;
req.session.user.email=user.email;
next();
})
.catch((err) => {
......@@ -83,5 +136,12 @@ function checkAuthenticated(req, res, next) {
}
router.get("/map", (req, res) => {
console.log(req.session.user);
res.render("map");
});
router.get('/logout',function(req,res){
req.session.destroy(); //세션비우기
res.redirect('/');
});
\ No newline at end of file
......
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1621187236305}
\ No newline at end of file
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1620823546959,"user":{"name":"‍김대철[학생](소프트웨어융합대학 컴퓨터공학과)","email":"kdc9619@khu.ac.kr","picture":"https://lh3.googleusercontent.com/a/AATXAJyP14ipRboJZ8T5-oNS3sRp4CG8wKCEwVABQ5G_=s96-c"}}
\ No newline at end of file
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1620684562564,"user":{"name":"‍최정민[학생](소프트웨어융합대학 컴퓨터공학과)","email":"cjm2021401@khu.ac.kr","picture":"https://lh3.googleusercontent.com/a/AATXAJzFD3R6CaL2NpysVrfvdGqcQ83vh_wpbN_5hj-0=s96-c"}}
\ No newline at end of file
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1621186188104}
\ No newline at end of file
{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1621191395860,"user":{"name":"‍최정민[학생](소프트웨어융합대학 컴퓨터공학과)","email":"cjm2021401@khu.ac.kr","nickname":"Mayf","age":"25","gender":"male"},"gender":"male"}
\ No newline at end of file
......@@ -22,8 +22,15 @@
<input type="submit" value="Submit">
</form>
<a href="/index" onclick="signOut();">Sign Out</a>
<a href="/logout" onclick="signOut();">Sign Out</a>
<%if (message=='same nickname'){%>
<script type="text/javascript">
alert("입력하신 NICKNAME은 사용하지 못합니다." +
"다른 NICKNAME을 입력하여 주세요");
</script>
<%}%>
</body>
<script>
......