최정민

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

-
This diff is collapsed. Click to expand it.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 "start": "nodemon ./bin/www" 6 "start": "nodemon ./bin/www"
7 }, 7 },
8 "dependencies": { 8 "dependencies": {
9 + "body-parser": "^1.19.0",
9 "cookie-parser": "~1.4.4", 10 "cookie-parser": "~1.4.4",
10 "debug": "~2.6.9", 11 "debug": "~2.6.9",
11 "ejs": "^3.1.6", 12 "ejs": "^3.1.6",
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
14 "google-auth-library": "^7.0.4", 15 "google-auth-library": "^7.0.4",
15 "http-errors": "~1.6.3", 16 "http-errors": "~1.6.3",
16 "morgan": "~1.9.1", 17 "morgan": "~1.9.1",
18 + "mysql": "^2.18.1",
17 "nodemon": "^2.0.7", 19 "nodemon": "^2.0.7",
18 "session-file-store": "^1.5.0" 20 "session-file-store": "^1.5.0"
19 } 21 }
......
1 var express = require("express"); 1 var express = require("express");
2 var router = express.Router(); 2 var router = express.Router();
3 +var bodyParser=require('body-parser');
3 var { OAuth2Client } = require("google-auth-library"); 4 var { OAuth2Client } = require("google-auth-library");
4 var CLIENT_ID = 5 var CLIENT_ID =
5 "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com"; 6 "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com";
6 var client = new OAuth2Client(CLIENT_ID); 7 var client = new OAuth2Client(CLIENT_ID);
8 +var mysql=require('mysql')
7 var session = require("express-session"); 9 var session = require("express-session");
8 var FileStore = require("session-file-store")(session); 10 var FileStore = require("session-file-store")(session);
11 +router.use(bodyParser.urlencoded({extended:false})); //url인코딩 x
12 +router.use(bodyParser.json()); //json방식으로 파
9 router.use( 13 router.use(
10 session({ 14 session({
11 secret: "209", // 암호화 15 secret: "209", // 암호화
...@@ -14,7 +18,13 @@ router.use( ...@@ -14,7 +18,13 @@ router.use(
14 store: new FileStore(), 18 store: new FileStore(),
15 }) 19 })
16 ); 20 );
17 - 21 +var connection = mysql.createConnection({
22 + host : 'localhost',
23 + user : 'root',
24 + password : 'g79465',
25 + database : 'caferecommend'
26 +});
27 +connection.connect();
18 /* GET home page. */ 28 /* GET home page. */
19 router.get("/", function (req, res, next) { 29 router.get("/", function (req, res, next) {
20 res.render("index", { 30 res.render("index", {
...@@ -44,16 +54,60 @@ router.post("/index", (req, res) => { ...@@ -44,16 +54,60 @@ router.post("/index", (req, res) => {
44 }); 54 });
45 55
46 router.get("/login", checkAuthenticated, (req, res) => { 56 router.get("/login", checkAuthenticated, (req, res) => {
47 - let user = req.user; 57 + var sql = 'SELECT * FROM USER WHERE EMAIL=?';
48 - req.session.user = user; 58 + var parameter=[req.session.user.email];
49 - res.render("login", { user: req.session.user }); 59 + connection.query(sql,parameter, function(err, row){
60 + if(err){
61 + console.log(err);
62 + }
63 + else {
64 + if (row.length > 0) {
65 + console.log("이미 가입이 되어있는 아이디")
66 + req.session.user.nickname = row[0].NICKNAME;
67 + req.session.user.age = row[0].AGE;
68 + req.session.user.gender = row[0].GENDER;
69 + return res.render('map', {user : req.session.user})
70 + }
71 + else{
72 + return res.render("login", { user: req.session.user, message:'none' });
73 + }
74 + }
75 + });
50 }); 76 });
51 77
52 router.post("/login", (req, res) => { 78 router.post("/login", (req, res) => {
53 console.log(req.body.nickname); 79 console.log(req.body.nickname);
54 console.log(req.body.age); 80 console.log(req.body.age);
55 console.log(req.body.gender); 81 console.log(req.body.gender);
56 - return res.render("login", { user: req.session.user }); 82 + var sql =' SELECT * FROM USER WHERE NICKNAME=?';
83 + var parameter=[req.body.nickname];
84 + connection.query(sql, parameter, function(err,row){
85 + if(err){
86 + console.log(err);
87 + }
88 + if(row.length>0){
89 + console.log('동일 닉네임있음');
90 +
91 + return res.render("login", {user : req.session.user, message: 'same nickname'});
92 + }
93 + else{
94 + req.session.user.nickname=req.body.nickname;
95 + req.session.user.age=req.body.age;
96 + req.session.user.gender=req.body.gender;
97 + var sql='INSERT INTO USER(EMAIL, NAME, NICKNAME, AGE, GENDER) VALUES(?,?,?,?,?)';
98 + var parameter=[req.session.user.email, req.session.user.name, req.session.user.nickname, req.session.user.age, req.session.user.gender];
99 + connection.query(sql, parameter, function(err, row){
100 + if(err){
101 + console.log(err);
102 + }
103 + else{
104 + console.log("새로운 user데이터 입력");
105 + }
106 + });
107 + return res.render("map", { user : req.session.user });
108 + }
109 + });
110 +
57 }); 111 });
58 module.exports = router; 112 module.exports = router;
59 113
...@@ -69,12 +123,11 @@ function checkAuthenticated(req, res, next) { ...@@ -69,12 +123,11 @@ function checkAuthenticated(req, res, next) {
69 const payload = ticket.getPayload(); 123 const payload = ticket.getPayload();
70 user.name = payload.name; 124 user.name = payload.name;
71 user.email = payload.email; 125 user.email = payload.email;
72 - user.picture = payload.picture;
73 - console.log(user.name);
74 } 126 }
75 verify() 127 verify()
76 .then(() => { 128 .then(() => {
77 - req.user = user; 129 + req.session.user.name = user.name;
130 + req.session.user.email=user.email;
78 next(); 131 next();
79 }) 132 })
80 .catch((err) => { 133 .catch((err) => {
...@@ -83,5 +136,12 @@ function checkAuthenticated(req, res, next) { ...@@ -83,5 +136,12 @@ function checkAuthenticated(req, res, next) {
83 } 136 }
84 137
85 router.get("/map", (req, res) => { 138 router.get("/map", (req, res) => {
139 + console.log(req.session.user);
86 res.render("map"); 140 res.render("map");
87 }); 141 });
142 +
143 +
144 +router.get('/logout',function(req,res){
145 + req.session.destroy(); //세션비우기
146 + res.redirect('/');
147 +});
...\ No newline at end of file ...\ No newline at end of file
......
1 +{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1621187236305}
...\ No newline at end of file ...\ No newline at end of file
1 -{"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 ...\ No newline at end of file
1 -{"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 ...\ No newline at end of file
1 +{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},"__lastAccess":1621186188104}
...\ No newline at end of file ...\ No newline at end of file
1 +{"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 ...\ No newline at end of file
...@@ -22,8 +22,15 @@ ...@@ -22,8 +22,15 @@
22 22
23 <input type="submit" value="Submit"> 23 <input type="submit" value="Submit">
24 </form> 24 </form>
25 -<a href="/index" onclick="signOut();">Sign Out</a> 25 +<a href="/logout" onclick="signOut();">Sign Out</a>
26 +<%if (message=='same nickname'){%>
27 + <script type="text/javascript">
26 28
29 + alert("입력하신 NICKNAME은 사용하지 못합니다." +
30 + "다른 NICKNAME을 입력하여 주세요");
31 +
32 + </script>
33 +<%}%>
27 34
28 </body> 35 </body>
29 <script> 36 <script>
......