dckat

카페 리스트 API 호출

1 +###초기설정
2 +>####데이터 베이스 설정
3 +> <pre><code>
4 +>CREATE TABLE USER(
5 +>ID int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
6 +>EMAIL varchar(64) NOT NULL UNIQUE,
7 +>NAME varchar(64) NOT NULL,
8 +>NICKNAME varchar(32) UNIQUE,
9 +>AGE varchar(32),
10 +>GENDER varchar(32)
11 +>);
12 +</code>
13 +</pre>
14 +
15 +###commit 형식
16 +<pre><code>
17 +##### 제목 - 50자 이내로 요약!
18 +
19 +### [커밋 타입]: [작업내용]
20 +
21 +##### 본문 - 한 줄에 최대 72 글자까지만 입력하기
22 +
23 +# 1. 무엇을 수정했는지
24 +# 2. 왜 수정했는지
25 +
26 +# 꼬릿말은 아래에 작성: ex) #이슈 번호
27 +-
28 +# [커밋 타입] 리스트
29 +# feat : 기능 (새로운 기능)
30 +# fix : 버그 (버그 수정)
31 +# refactor : 리팩토링
32 +# style : 스타일 (코드 형식, 세미콜론 추가: 비즈니스 로직에 변경 없음)
33 +# docs : 문서 (문서 추가, 수정, 삭제)
34 +# test : 테스트 (테스트 코드 추가, 수정, 삭제: 비즈니스 로직에 변경 없음)
35 +# chore : 기타 변경사항 (빌드 스크립트 수정 등)
36 +# post : 블로그 포스트 추가 (신규 포스트 작성 및 수정)
37 +# ------------------
38 +# [체크리스트]
39 +# 제목 첫 글자는 대문자로 작성했나요?
40 +</code>
41 +</pre>
42 +
43 +
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 "request": "^2.88.2", 20 "request": "^2.88.2",
19 "session-file-store": "^1.5.0" 21 "session-file-store": "^1.5.0"
......
1 var express = require("express"); 1 var express = require("express");
2 var router = express.Router(); 2 var router = express.Router();
3 +
3 var request = require("request"); 4 var request = require("request");
5 +var bodyParser=require('body-parser');
4 6
5 var { OAuth2Client } = require("google-auth-library"); 7 var { OAuth2Client } = require("google-auth-library");
6 var querystring = require("querystring"); 8 var querystring = require("querystring");
...@@ -8,8 +10,11 @@ var querystring = require("querystring"); ...@@ -8,8 +10,11 @@ var querystring = require("querystring");
8 var CLIENT_ID = 10 var CLIENT_ID =
9 "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com"; 11 "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com";
10 var client = new OAuth2Client(CLIENT_ID); 12 var client = new OAuth2Client(CLIENT_ID);
13 +var mysql=require('mysql')
11 var session = require("express-session"); 14 var session = require("express-session");
12 var FileStore = require("session-file-store")(session); 15 var FileStore = require("session-file-store")(session);
16 +router.use(bodyParser.urlencoded({extended:false})); //url인코딩 x
17 +router.use(bodyParser.json()); //json방식으로 파
13 router.use( 18 router.use(
14 session({ 19 session({
15 secret: "209", // 암호화 20 secret: "209", // 암호화
...@@ -18,7 +23,13 @@ router.use( ...@@ -18,7 +23,13 @@ router.use(
18 store: new FileStore(), 23 store: new FileStore(),
19 }) 24 })
20 ); 25 );
21 - 26 +var connection = mysql.createConnection({
27 + host : 'localhost',
28 + user : 'root',
29 + password : 'g79465',
30 + database : 'caferecommend'
31 +});
32 +connection.connect();
22 /* GET home page. */ 33 /* GET home page. */
23 router.get("/", function (req, res, next) { 34 router.get("/", function (req, res, next) {
24 res.render("index", { 35 res.render("index", {
...@@ -48,16 +59,60 @@ router.post("/index", (req, res) => { ...@@ -48,16 +59,60 @@ router.post("/index", (req, res) => {
48 }); 59 });
49 60
50 router.get("/login", checkAuthenticated, (req, res) => { 61 router.get("/login", checkAuthenticated, (req, res) => {
51 - let user = req.user; 62 + var sql = 'SELECT * FROM USER WHERE EMAIL=?';
52 - req.session.user = user; 63 + var parameter=[req.session.user.email];
53 - res.render("login", { user: req.session.user }); 64 + connection.query(sql,parameter, function(err, row){
65 + if(err){
66 + console.log(err);
67 + }
68 + else {
69 + if (row.length > 0) {
70 + console.log("이미 가입이 되어있는 아이디")
71 + req.session.user.nickname = row[0].NICKNAME;
72 + req.session.user.age = row[0].AGE;
73 + req.session.user.gender = row[0].GENDER;
74 + return res.render('map', {user : req.session.user})
75 + }
76 + else{
77 + return res.render("login", { user: req.session.user, message:'none' });
78 + }
79 + }
80 + });
54 }); 81 });
55 82
56 router.post("/login", (req, res) => { 83 router.post("/login", (req, res) => {
57 console.log(req.body.nickname); 84 console.log(req.body.nickname);
58 console.log(req.body.age); 85 console.log(req.body.age);
59 console.log(req.body.gender); 86 console.log(req.body.gender);
60 - return res.render("login", { user: req.session.user }); 87 + var sql =' SELECT * FROM USER WHERE NICKNAME=?';
88 + var parameter=[req.body.nickname];
89 + connection.query(sql, parameter, function(err,row){
90 + if(err){
91 + console.log(err);
92 + }
93 + if(row.length>0){
94 + console.log('동일 닉네임있음');
95 +
96 + return res.render("login", {user : req.session.user, message: 'same nickname'});
97 + }
98 + else{
99 + req.session.user.nickname=req.body.nickname;
100 + req.session.user.age=req.body.age;
101 + req.session.user.gender=req.body.gender;
102 + var sql='INSERT INTO USER(EMAIL, NAME, NICKNAME, AGE, GENDER) VALUES(?,?,?,?,?)';
103 + var parameter=[req.session.user.email, req.session.user.name, req.session.user.nickname, req.session.user.age, req.session.user.gender];
104 + connection.query(sql, parameter, function(err, row){
105 + if(err){
106 + console.log(err);
107 + }
108 + else{
109 + console.log("새로운 user데이터 입력");
110 + }
111 + });
112 + return res.render("map", { user : req.session.user });
113 + }
114 + });
115 +
61 }); 116 });
62 117
63 router.get("/cafe", (req, res) => { 118 router.get("/cafe", (req, res) => {
...@@ -104,15 +159,28 @@ function checkAuthenticated(req, res, next) { ...@@ -104,15 +159,28 @@ function checkAuthenticated(req, res, next) {
104 const payload = ticket.getPayload(); 159 const payload = ticket.getPayload();
105 user.name = payload.name; 160 user.name = payload.name;
106 user.email = payload.email; 161 user.email = payload.email;
107 - user.picture = payload.picture;
108 - console.log(user.name);
109 } 162 }
110 verify() 163 verify()
111 .then(() => { 164 .then(() => {
112 - req.user = user; 165 + req.session.user.name = user.name;
166 + req.session.user.email=user.email;
113 next(); 167 next();
114 }) 168 })
115 .catch((err) => { 169 .catch((err) => {
116 res.redirect("/index"); 170 res.redirect("/index");
117 }); 171 });
118 } 172 }
173 +<<<<<<< HEAD
174 +=======
175 +
176 +router.get("/map", (req, res) => {
177 + console.log(req.session.user);
178 + res.render("map");
179 +});
180 +
181 +
182 +router.get('/logout',function(req,res){
183 + req.session.destroy(); //세션비우기
184 + res.redirect('/');
185 +});
186 +>>>>>>> 725445bde095435f47f8bc948873818794f00365
......
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>
......