-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
470 lines (468 loc) · 15.4 KB
/
server.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
require("dotenv").config();
const { google } = require("googleapis");
const express = require("express");
const app = express();
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const morgan = require("morgan");
const mongoose = require("mongoose");
const errorHandler = require("errorhandler");
const cors = require("cors");
const userModel = require("./models/userModel");
const tempUserModel = require("./models/tempUserModel");
const nodemailer = require("nodemailer");
const cookieParser = require("cookie-parser");
const crypto = require("crypto");
const PORT = process.env.PORT || 8080;
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.EMAIL_PASSWORD,
},
});
app.use(cors({ origin: "http://localhost:3000", credentials: true }));
app.use(cookieParser());
app.use(express.json());
app.use(morgan("dev"));
mongoose.connect(
`mongodb+srv://${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@reviewerdb.n4mee.mongodb.net/USERS?retryWrites=true&w=majority`,
{ useUnifiedTopology: true, useNewUrlParser: true },
(error) => {
if (error) {
console.log("Trouble Connecting to USERS DB", error);
} else {
console.log("Connected to USERS DB");
}
},
);
const verifyRefreshToken = (req, res, next) => {
const access_token = req.headers.authorization.split(" ")[1];
if (access_token) {
jwt.verify(access_token, process.env.ACCESS_TOKEN_SECRET, (err, id) => {
if (err && err.name === "TokenExpiredError") {
const sessionID = req.headers.sessionID;
const refresh_token = req.cookies[`refresh_token${sessionID}`];
if (refresh_token) {
jwt.verify(refresh_token, process.env.REFRESH_TOKEN_SECRET, (err, id) => {
if (err) {
res.status(401).send("refresh token not verified");
} else {
userModel.findById(id.sub, (err, doc) => {
if (err) {
res.status(404).send("Cannot find user");
} else {
if (doc && doc.refreshTokens.includes(refresh_token)) {
const new_access_token = jwt.sign(
{ sub: doc._id },
process.env.ACCESS_TOKEN_SECRET,
{
expiresIn: "10m",
},
);
req.headers.authorization = `Bearer ${new_access_token}`;
console.log("Created new access token");
next();
} else {
res.status(401).send("Refresh token doesn't match");
}
}
});
}
});
} else {
res.sendStatus(401);
}
} else {
next();
}
});
} else {
res.send(401);
}
};
app.post("/users/create", async (req, res, next) => {
try {
userModel.find({ email: req.body.email }, async (err, doc) => {
if (err) {
res.sendStatus(500);
} else if (doc[0]) {
res.sendStatus(403);
} else {
const encryptedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = {
username: req.body.username,
email: req.body.email,
password: encryptedPassword,
};
let newTempUser = tempUserModel(newUser);
newTempUser.save((error, doc) => {
if (error) {
console.log("Cannot add temp user to db", error);
} else {
console.log("Successfully added user");
newTempUser = doc;
}
});
const access_token = jwt.sign({ sub: newTempUser._id }, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "12h",
});
const url = `http://localhost:3000/verify/${access_token}`;
const mailOptions = {
to: `${req.body.email}`,
from: process.env.EMAIL,
subject: "Email Verification Link",
html: `<div style='background:linear-gradient(90deg, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);;width:60%;margin:20px auto;padding:50px;'><h1 style="display:block;color:white; margin:20px;font-family:sans-serif">Hi,<h1><h3 style="display:block;margin:20px;font-family:sans-serif;color:white">This is just an email verification mail for ${req.body.username}<h3><br><br><a href=${url}><button style="display:block;padding:20px 30px;background-color:white;color:#090979;font-size:16px;font-weight:bold;border:none;border-radius:10px;margin:0px 20px;">Click Here</button></a></div>`,
};
transporter.sendMail(mailOptions, (err) => {
if (err) {
console.log("Error occurred ", err);
} else {
console.log(`Sent email to ${req.body.email}`);
}
});
res.sendStatus(201);
}
});
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
app.get("/login_successful/:token", (res, req, next) => {
const tokens = fetch(`http://localhost:8080/verify/${req.params.token}`).then((response) => {
if (response.ok) {
return response;
} else {
res.sendStatus(403);
}
});
if (tokens) {
res.status(200).send(tokens).redirect();
}
});
app.post("/refresh_token", (req, res, next) => {
const sessionID = req.body.sessionID;
const refresh_token = req.cookies[`refresh_token${sessionID}`];
if (refresh_token) {
jwt.verify(refresh_token, process.env.REFRESH_TOKEN_SECRET, (err, id) => {
if (err) {
res.status(401).send("refresh token not verified");
} else {
userModel.findById(id.sub, (err, doc) => {
if (err) {
res.status(404).send("Cannot find user");
} else {
if (doc && doc.refreshTokens.includes(refresh_token)) {
const new_access_token = jwt.sign({ sub: doc._id }, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "15m",
});
res.json(new_access_token);
} else {
res.status(401).send("Refresh token doesn't match");
}
}
});
}
});
} else {
res.status(401).send("No refresh_token");
}
});
app.post("/login", async (req, res, next) => {
userModel.find({ email: req.body.email }, async (err, doc) => {
if (err) {
res.sendStatus(404);
} else {
if (doc[0]) {
const userId = doc[0]._id;
const userPassword = doc[0].password;
const refresh_token_array = doc[0].refreshTokens;
try {
const result = await bcrypt.compare(req.body.pass, userPassword);
if (result) {
const refresh_token = jwt.sign({ sub: userId }, process.env.REFRESH_TOKEN_SECRET);
refresh_token_array.push(refresh_token);
userModel.findByIdAndUpdate(
userId,
{ refreshTokens: refresh_token_array },
{ new: true },
(err, result) => {
if (err) {
console.log("Cannot update refresh token");
res.sendStatus(401);
} else {
const access_token = jwt.sign({ sub: userId }, process.env.ACCESS_TOKEN_SECRET);
const sessionID = crypto.randomBytes(32).toString("hex");
res.cookie(`refresh_token${sessionID}`, refresh_token, { httpOnly: true });
res.status(200).send({
password: "Success",
access_token: access_token,
sessionID: sessionID,
});
}
},
);
} else {
res.sendStatus(401);
}
} catch {
console.log(doc);
res.sendStatus(500);
}
} else {
res.sendStatus(404);
}
}
});
});
app.post("/verify", (req, res, next) => {
const access_token = req.headers.authorization.split(" ")[1];
if (!access_token) {
res.status(401).send("No access token");
}
jwt.verify(access_token, process.env.ACCESS_TOKEN_SECRET, (err, doc) => {
if (err) {
res.status(401).send("access token not verified");
} else {
const id = doc.sub;
tempUserModel.findById(id, (err, doc) => {
if (err) {
res.status(401).send("not found in temp user model");
} else {
const tempUserId = doc._id;
const obj = {
username: doc.username,
email: doc.email,
password: doc.password,
};
const newUser = userModel(obj);
newUser.save((error, user) => {
if (error) {
console.log("Cannot add permanent user", error);
} else {
const userId = { sub: user._id };
const access_token = jwt.sign(userId, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "15m",
});
const refresh_token = jwt.sign(userId, process.env.REFRESH_TOKEN_SECRET);
const refresh_token_array = [refresh_token];
userModel.findByIdAndUpdate(
user._id,
{ refreshTokens: refresh_token_array },
{ new: true },
(error, result) => {
if (error) {
console.log("Cannot save the refresh token");
} else {
tempUserModel.findByIdAndDelete(tempUserId, (err, doc) => {
if (err) {
console.log("Cannot delete Temporary User");
res.sendStatus(500);
}
});
const sessionID = crypto.randomBytes(32).toString("hex");
res.cookie(`refresh_token${sessionID}`, refresh_token, { httpOnly: true });
return res
.status(201)
.send({ access_token: access_token, sessionID: sessionID });
}
},
);
}
});
}
});
}
});
});
app.get("/user", verifyRefreshToken, (req, res, next) => {
const access_token = req.headers.authorization.split(" ")[1];
const id = jwt.verify(access_token, process.env.ACCESS_TOKEN_SECRET, (err, result) => {
if (err) {
console.log("Couldn't verify access token ");
res.sendStatus(403);
} else {
return result.sub;
}
});
userModel.findById(id, (err, doc) => {
if (err) {
console.log("Cannot find user with the given ID", err);
res.sendStatus(404);
} else {
res
.status(200)
.send({ username: doc.username, email: doc.email, access_token: access_token });
}
});
});
app.post("/logout", (req, res, next) => {
const access_token = req.headers.authorization.split(" ")[1];
const sessionID = req.body.sessionID;
const current_refresh_token = req.cookies[`refresh_token${sessionID}`];
jwt.verify(access_token, process.env.ACCESS_TOKEN_SECRET, (err, id) => {
if (err) {
console.log(err);
res.sendStatus(401);
} else {
const userId = id["sub"];
userModel.findById(userId, (err, doc) => {
if (err) {
res.sendStatus(404);
} else {
const refresh_token_array = doc.refreshTokens.filter((token) => {
return current_refresh_token != token;
});
userModel.findByIdAndUpdate(userId, { refreshTokens: refresh_token_array }, (err) => {
if (err) {
console.log("Did not log out successfully");
res.sendStatus(401);
} else {
console.log("Logged Out successfully");
res.clearCookie(`refresh_token${sessionID}`);
res.status(200).send({ message: "logged out" });
}
});
}
});
}
});
});
app.post("/forgot_password", (req, res, next) => {
const email = req.body.email;
userModel.find({ email: email }, (err, doc) => {
if (err) {
res.sendStatus(404);
} else {
if (doc[0]) {
const id = doc[0]._id;
const access_token = jwt.sign({ sub: id }, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "30m",
});
const url = `http://localhost:3000/reset_password/${access_token}`;
const mailOptions = {
to: `${email}`,
from: process.env.EMAIL,
subject: "Password Change Request",
html: `<div style='background-color: #FBAB7E;background-image: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%);width:60%;margin:20px auto;padding:50px;'><h1 style="display:block;color:white; margin:20px;font-family:sans-serif">Hi,<h1><h3 style="display:block;margin:20px;font-family:sans-serif;color:white">This is a password reset request for ${doc[0].username}<h3><br><br><a href=${url}><button style="display:block;padding:20px 30px;background-color:white;color:#090979;font-size:16px;font-weight:bold;border:none;border-radius:10px;margin:0px 20px;">Click Here</button></a></div>`,
};
transporter.sendMail(mailOptions, (err) => {
if (err) {
console.log("Cannot send the mail");
} else {
console.log(`Sent the email to ${email}`);
res.status(200).send({ message: `Sent the password change email to ${email}` });
}
});
} else {
res.sendStatus(404);
}
}
});
});
app.post("/reset_password", async (req, res, next) => {
const access_token = req.headers.authorization.split(" ")[1];
jwt.verify(access_token, process.env.ACCESS_TOKEN_SECRET, async (err, result) => {
if (err) {
console.log(err);
res.sendStatus(401);
} else {
const id = result["sub"];
const encryptedPassword = await bcrypt.hash(req.body.newPassword, 10);
console.log("User ID", id);
userModel.findByIdAndUpdate(id, { password: encryptedPassword }, { new: true }, (err) => {
if (err) {
res.sendStatus(404);
} else {
res.status(200).send({ message: "changed successfully" });
}
});
}
});
});
const googleConfig = {
clientId: process.env.GOOGLE_CLIENT_ID, // e.g. asdfghjkljhgfdsghjk.apps.googleusercontent.com
clientSecret: process.env.GOOGLE_CLIENT_SECRET, // e.g. _ASDFA%DFASDFASDFASD#FAD-
redirect: process.env.GOOGLE_REDIRECT_URL, // this must match your google api settings
};
const defaultScope = [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile",
];
const oauth2client = new google.auth.OAuth2(
googleConfig.clientId,
googleConfig.clientSecret,
googleConfig.redirect,
);
app.get("/generate_url", async (req, res, next) => {
const url = await oauth2client.generateAuthUrl({
access_type: "offline",
prompt: "consent",
scope: defaultScope,
});
res.status(200).send({ authURL: url });
});
app.post("/new_google_login", async (req, res, next) => {
const code = req.body.code;
const { tokens } = oauth2client.getToken(code);
const payload = await jwt.decode(tokens.id_token);
const username = payload.name;
const email = payload.email;
userModel.find({ email: email }, (err, doc) => {
if (err) {
res.sendStatus(403);
} else if (doc[0]) {
const userID = doc[0]._id;
const refresh_token_array = doc[0].refreshTokens;
const refresh_token = jwt.sign({ sub: userID }, process.env.REFRESH_TOKEN_SECRET);
refresh_token_array.push(refresh_token);
console.log("Sign-in user ", refresh_token_array);
userModel.findByIdAndUpdate(
userID,
{ refreshTokens: refresh_token_array },
{ new: true },
(err, result) => {
if (err) {
console.log("Cannot update refresh tokens", err);
} else {
console.log("new Document", result);
}
},
);
const sessionID = crypto.randomBytes(32).toString("hex");
res.cookie(`refresh_token${sessionID}`, refresh_token, { httpOnly: true });
const access_token = jwt.sign({ sub: userID }, process.env.ACCESS_TOKEN_SECRET);
res.status(200).send({ access_token: access_token, sessionID: sessionID });
} else {
const newUser = userModel({ email: email, username: username });
newUser.save((err, doc) => {
if (err) {
console.log("Cannot save in DB");
res.send(500);
} else {
console.log("Saved in user DB");
const userID = doc._id;
const refresh_token = jwt.sign({ sub: userID }, process.env.REFRESH_TOKEN_SECRET);
const access_token = jwt.sign({ sub: userID }, process.env.ACCESS_TOKEN_SECRET, {
expiresIn: "15m",
});
const refresh_token_array = [refresh_token];
const sessionID = crypto.randomBytes(32).toString("hex");
res.cookie(`refresh_token${sessionID}`, refresh_token, { httpOnly: true });
userModel.findByIdAndUpdate(userID, { refreshTokens: refresh_token_array }, (err) => {
if (err) {
console.log("Cannot save the refresh token in DB");
res.send(500);
}
});
res.status(201).send({ access_token: access_token, sessionID: sessionID });
}
});
}
});
});
app.use(errorHandler());
app.listen(PORT, () => {
console.log(`Listening at port ${PORT}`);
});