index.js
5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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");
const session = require("express-session");
const FileStore = require("session-file-store")(session);
router.use(bodyParser.urlencoded({ extended: false })); //url인코딩 x
router.use(bodyParser.json()); //json방식으로 파싱
router.use(session({
secret: '209', // 암호화
resave: false,
saveUninitialized: true,
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", {
d: "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com",
});
});
router.get("/index", function (req, res, next) {
res.render("index", {
d: "94679084723-s5f0686p2porp9mkakrp1p89a48n24nj.apps.googleusercontent.com",
});
});
router.post("/index", (req, res) => {
let token = req.body.token;
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
});
const payload = ticket.getPayload();
const userid = payload["sub"];
}
verify()
.then(() => {
res.cookie("session-token", token);
res.send("success");
})
.catch(console.error);
});
router.get("/login", checkAuthenticated, (req, res) => {
req.session.user =req.user;
req.session.user.email = req.user.email;
req.session.user.name=req.user.name;
req.session.user.picture=req.user.picture;
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("구글로그인 성공");
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) {
if (err) {
console.log(err);
} else {
console.log("새로운 user데이터 입력");
}
});
return res.render("map", { user: req.session.user });
}
});
});
function checkAuthenticated(req, res, next) {
let token = req.cookies["session-token"];
let user = {};
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
});
const payload = ticket.getPayload();
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
}
verify()
.then(() => {
req.user = user;
next();
})
.catch((err) => {
res.redirect("/index");
});
}
router.get("/map", (req, res) => {
if (req.session.user) {
res.render("map", {user:req.session.user});
}
res.render("map");
});
router.get("/logout", function (req, res) {
req.session.destroy(); //세션비우기
res.redirect("/");
});
router.get("/comment/:cafeId", function (req, res) {
const cafeId = req.params.cafeId;
res.render("comment", { cafeId: cafeId });
});
// 카페 후기 등록
router.post("/comment", function (req, res) {
var cafeId = req.body.cafeId;
var price = req.body.price;
var kindness = req.body.kindness;
var noise = req.body.noise;
var accessibility = req.body.accessibility;
console.log(price);
console.log(kindness);
console.log(noise);
console.log(accessibility);
// 입력받지 않은 데이터가 하나라도 존재 (카페아이디는 후기작성시 자동으로 받아옴)
if (!cafeId || !price || !kindness || !noise || !accessibility) {
console.log("입력받지 않은 데이터 존재");
res.redirect("/comment/:cafeId"); // 후기작성으로 다시 이동
}
var sql =
"INSERT INTO COMMENT(CAFE_ID, PRICE, KINDNESS, NOISE, ACCESSIBILITY) VALUES(?,?,?,?,?)";
var parameter = [cafeId, price, kindness, noise, accessibility];
connection.query(sql, parameter, function (err) {
if (err) {
console.log(err);
} else {
console.log("새로운 comment 데이터 입력");
}
});
return res.render("map");
});
module.exports = router;