-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
352 lines (325 loc) · 9.81 KB
/
index.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// npm modules
const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const moment = require("moment-timezone");
const nodemailer = require("nodemailer");
const { v4: uuidv4 } = require("uuid");
const jwt = require("jsonwebtoken");
const crypto = require("crypto");
const multer = require("multer");
const argon2 = require("argon2");
const helmet = require('helmet');
const mysql = require("mysql2");
const path = require("path");
const http = require("http");
const hbs = require("hbs");
const fs = require("fs");
//--
// custom modules
const mymailer = require("./modules/mymailer");
const userDB = require("./modules/user_db");
//--
const server_port = 3000;
const app = express();
const server = http.createServer(app);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser(process.env["session_key"]));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/public", express.static(__dirname + "/public"));
const JWTsecretKey = process.env["session_key"];
app.set("view engine", "hbs");
app.set("views", __dirname + "/monopoly");
function verifyToken(req, res, next) {
const token = req.headers["authorization"];
if (token) {
jwt.verify(token, JWTsecretKey, (err, decoded) => {
if (err) {
req.tokenData = {status: false, reason: 'server error'};
next();
} else {
const currentTime = moment().tz("Asia/Seoul").unix();
if(decoded.expiresIn < currentTime) {
req.tokenData = {status: false, reason: 'token expired'};
next();
} else {
req.tokenData = {status: true, data: decoded};
next();
}
}
});
} else {
req.tokenData = {status: false, reason: 'token not found'};
next();
}
}
app.post('/protection', verifyToken, (req, res) => {
if(!req.tokenData) return res.json({status: false});
if(!req.tokenData.status) return res.json(req.tokenData);
return res.json({status: true});
});
app.get("/", verifyToken, async (req, res) => {
res.sendFile(path.join(__dirname, "public", "home", "html", "home.html"));
});
app.post("/send-reset-password-mail", async (req, res) => {
try {
const email = req.body.email;
if (email) {
const user = await userDB.getUser({ email: email }, ["id"]);
if (!user.status) {
return res.json({
status: false,
reason: "User not found.",
});
}
const userId = user.data.id;
const tokenInfo = await userDB.getPasswordResetTokenInfo({ user_id: userId }, ["id"],);
let saveResult;
if (!tokenInfo) {
return res.json({ status: false, reason: "Token not found." });
}
if (tokenInfo.status) {
const token = crypto.randomBytes(20).toString("hex");
const koreaTime = moment().tz("Asia/Seoul");
const expiration_date = koreaTime.add(1, "days").toDate();
saveResult = await userDB.updatePasswordResetToken({ token, expiration_date },{ user_id: userId },);
saveResult.token = token;
} else {
saveResult = await userDB.savePasswordResetToken(userId);
}
if (saveResult.status) {
mymailer.send(email,"passwordReset",{ token: saveResult.token }, function (result) {
return res.json(result);
});
} else {
return res.json({
status: false,
reason: "server error",
});
}
} else {
return res.json({
status: false,
reason: "email is required",
});
}
} catch (error) {
console.log("send-reset-password-mail error: " + JSON.stringify(error));
res.json(error);
}
});
app.post("/reset-password", async (req, res) => {
const password = req.body.password;
const recheckPassword = req.body.recheckPassword;
const token = req.body.token;
if (!password || !recheckPassword || !token) {
return res.json({
status: false,
reason: "password, recheckPassword, token is required.",
});
}
const tokenInfo = await userDB.getPasswordResetTokenInfo({ token }, ["id", "token", "user_id", "expiration_date", "used",]);
if (!tokenInfo) {
return res.json({ status: false, reason: "Token not found." });
}
if (!tokenInfo.status) {
return res.json({
status: false,
reason: "token is invalid",
});
}
const tokenUserId = tokenInfo.data.user_id;
const tokenExpiryDate = tokenInfo.data.expiration_date;
if (tokenInfo.data.used === 1) {
return res.json({
status: false,
reason: "token is invalid",
});
}
if (tokenExpiryDate < moment().tz("Asia/Seoul").toDate()) {
return res.json({
status: false,
reason: "token is expired",
});
}
if (password === recheckPassword) {
userDB
.getUser({ id: tokenUserId }, ["id"])
.then(async (user) => {
if (user) {
const tokenUpdate = await userDB.updatePasswordResetToken({ used: 1 },{ id: tokenInfo.data.id },);
if (!tokenUpdate.status) {
return res.json({
status: false,
reason: "server error",
});
}
argon2.hash(password).then((hashedPassword) => {
userDB
.updateUser(user.data.id, { password: hashedPassword }, ["password",])
.then((result) => {
res.json(result);
})
.catch((error) => {
console.error(error);
return res.json({
status: false,
reason: "서버 오류입니다. 잠시 후 다시 시도해주세요.",
});
});
});
} else {
console.error(error);
return res.json({
status: false,
reason: "서버 오류입니다. 잠시 후 다시 시도해주세요.",
});
}
})
.catch((error) => {
console.log(error);
});
} else {
return res.json({
status: false,
reason: "password is not matched",
});
}
});
app.get("/reset/token=:token", async (req, res) => {
const token = req.params.token;
const DBtoken = await userDB.getPasswordResetTokenInfo({ token }, ["id"]);
if (!DBtoken.status) {
const data = errcase(403);
res.render("error_page/error", data);
} else {
res.sendFile(path.join(__dirname, "/public/login/reset_password/html/index.html"));
}
});
app.get("/login", (req, res) => {
res.sendFile(path.join(__dirname, "/public/login/html/login.html"));
});
app.post("/sign-in", async (req, res) => {
try {
const { email, password, remember } = req.body;
const signinData = await userDB.signin(email, password);
if (!signinData) {
return res.json({
status: false,
reason: "email or password is wrong.",
});
}
const userInfo = await userDB.getUser({ email }, ["id"]);
const tokenExpiration = remember ? 30 : 1;
const expiryDate = moment().add(tokenExpiration, "days").tz("Asia/Seoul").unix();
if (userInfo.status) {
const token = jwt.sign(
{ userId: userInfo.data.id, },
JWTsecretKey,
{ expiresIn: expiryDate, },
);
return res.json({ status: true, token });
} else {
return res.json(userInfo);
}
} catch (error) {
console.error(error);
res.json(error);
}
});
app.post("/sign-up", async (req, res) => {
const name = req.body.username;
const email = req.body.email;
const password = req.body.password;
const password_recheck = req.body.password_recheck;
userDB
.signup(name, email, password, password_recheck)
.then((result) => {
return res.json(result);
})
.catch((error) => {
console.error(error);
return res.json(error);
});
});
app.get("/mypage", async (req, res) => {
try {
res.sendFile(path.join(__dirname, "/public/user/html/mypage.html"));
} catch (error) {
console.error(error);
return res.json({
status: false,
reason: "서버 오류",
});
}
});
app.post("/token_user_info", verifyToken, async (req, res) => {
try {
const token = req.tokenData;
if (!token.status) return res.json({ status: false });
const user = await userDB.getUser({ id: token.data.userId }, ["id","username","email"]);
return res.json(user);
} catch (error) {
console.error(error);
return res.json({
status: false,
reason: "서버 오류",
});
}
});
function errcase(errCode) {
switch (errCode) {
case 400:
return {
title: `${errCode} Error`,
h: `${errCode} Bad Request`,
p: "요청이 유효하지 않거나 부적절합니다.",
};
case 401:
return {
title: `${errCode} Error`,
h: `${errCode} Unauthorized`,
p: "클라이언트 요청이 완료되지 않았습니다.",
};
case 403:
return {
title: `${errCode} Error`,
h: `${errCode} Access denied`,
p: "요청이 거부되었습니다.",
};
case 404:
return {
title: `${errCode} Error`,
h: `${errCode} Not Found`,
p: "요청한 페이지를 찾을 수 없습니다.",
};
case 500:
return {
title: `${errCode} Error`,
h: `${errCode} Internal Server Error`,
p: "서버에서 오류가 발생하였습니다.",
};
case 503:
return {
title: `${errCode} Error`,
h: `${errCode} Maintenance`,
p: "현재 이 페이지는 점검 중입니다.",
};
default:
return {
title: "Error",
h: `${errCode} Unexpected`,
p: "예상치 못한 오류입니다.",
};
}
}
app.get("*", function (req, res) {
const data = errcase(404);
res.render("error_page/error", data);
});
server.listen(server_port, () => {
console.log("Server is running on port 3000");
});