-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
414 lines (372 loc) · 12.2 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
const express = require("express");
const crypto = require('crypto');
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const bodyParser = require("body-parser");
const async = require("async");
const nodemailer = require("nodemailer");
const sgMail = require("@sendgrid/mail");
const {DateTime} = require("luxon");
const sgTransport = require("nodemailer-sendgrid-transport");
const mysql = require("mysql");
const axios = require("axios");
require('dotenv').config();
const app = express();
app.use(bodyParser.json());
const connection = mysql.createConnection({
host: "localhost",
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "ghostedDB",
});
connection.connect();
app.get("/api", (req, res) => {
res.json({
message: "Welcome to the api",
});
});
app.post("/register", (req, res) => {
const username = req.body.username;
const compareUsername = "SELECT * FROM ghosted WHERE username=?";
connection.query(compareUsername, [username], async function (err, results) {
if (err) throw err;
if (results < 1) {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const user = {
name: req.body.username,
password: hashedPassword,
email: req.body.email,
};
// Get data into DB
const username = user.name;
const pass = user.password;
const mail = user.email;
const sqlQuery =
"INSERT INTO `ghosted` (`username`, `email`, `password`) VALUES (?,?,?);";
connection.query(
sqlQuery,
[username, mail, pass],
function (err, result) {
if (err) throw err;
console.log("User added");
res.status(201).send({response: "Account was created successfully", statusC: "201"});
}
);
} catch {
res.status(500).send({
response: "There was a problem while creating your account.",
statusC: "500",
});
}
} else {
console.log("Already exists");
res.status(200).send({
response: "Username already exists",
statusC: "200",
});
}
});
});
app.post("/login", async (req, res) => {
const userID = req.body.username;
const getDataLogin = "SELECT * FROM ghosted WHERE username=?";
connection.query(getDataLogin, [userID], async function (err, results) {
if(err) throw err;
if(results.length >= 1) {
if (await bcrypt.compare(req.body.password, results[0].password)) {
jwt.sign({ user: req.body.username }, process.env.ACCESS_TOKEN_SECRET, (err, token) => {
res.json({
id: results[0].id,
email: results[0].email,
token: token,
login: true,
});
});
} else {
res.status(403).send({
response: "The password is incorrect. Please try again.",
statusC: "403"
});
}
} else {
res.status(200).send({
response: "Username not found.",
statusC: "404",
})
};
})
});
app.post("/forgot/pass", async (req, res, next) => {
const email = req.body.email;
async.waterfall([
function (done) {
crypto.randomBytes(3,function (err, buf) {
const token = buf.toString("hex");
done(err, token);
});
},
function (token, done) {
const resetPasswordToken = token;
const resetPasswordExpires = Math.floor(Date.now() / 1000); // 1 hour
console.log(resetPasswordExpires);
const addToken = 'UPDATE ghosted SET resetPasswordToken=?, resetPasswordExpires=? WHERE email=?;'
connection.query(addToken, [resetPasswordToken, resetPasswordExpires, email], function(err, result) {
if(err) throw err;
console.log('Added token and expiry date for ' + email);
})
sgMail.setApiKey(process.env.API_KEY_SENDMAIL);
const options = {
service: 'SendGrid',
auth: {
api_user: process.env.USER_SENDMAIL,
api_key: process.env.PASS_SENDMAIL,
}
}
const client = nodemailer.createTransport(sgTransport(options));
const mailOptions = {
from: '[email protected]',
to: email,
subject: 'Ghosted Change Password',
html: "You are receiving this because you (or someone else) have requested the reset of the password for this account. <br/><br/>" +
"Please write or paste the following code in the corresponsing field box to complete the verification. <br/><br/>" +
"<p style='font-weight: bold; font-size: 22px;'>" + resetPasswordToken + "</p>" +
"<b><u><i>The code only works for one hour (1 hour).</i></u></b> <br/><br/><br/>" +
"If you (or someone you know) did not request this, please ignore this email and your password will remain unchanged." +
"<p>© Ghosted | 2020</p>"
};
const userCheck = "SELECT * FROM ghosted WHERE email=?";
connection.query(userCheck, [email], function(err, result) {
if(err) throw err;
console.log(result);
if(result.length >= 1) {
sgMail.send(mailOptions).then(() => {
console.log('Email sent');
})
.catch((error) => {
console.log(error)
})
}
})
res.status(200).send({
response: "If you have an account with us we've sent an email",
statusC: "200"
})
}
]);
})
app.post("/forgot/verify", async (req,res) => {
const currentTime = Math.floor(Date.now() / 1000);
const token = req.body.token;
console.log(currentTime, token);
const selectData = "SELECT * FROM ghosted WHERE resetPasswordToken=?;";
connection.query(selectData, [token], function(err, result) {
if(err) throw err;
if(result.length < 1) {
res.sendStatus(404);
} else if(result[0].resetPasswordExpires+3600 > currentTime) {
const updatePassword = "UPDATE ghosted SET password=? WHERE resetPasswordToken=?;";
const pass = req.body.password;
async function insertPassword() {
const hashedPass = await bcrypt.hash(pass, 10);
connection.query(updatePassword, [hashedPass, token], function(err, results) {
if(err) throw err;
console.log('Password Updated');
res.status(200).send({ response: "Password was updated successfully", statusC: "200"});
});
sgMail.setApiKey(process.env.API_KEY_SENDMAIL);
const options = {
service: "SendGrid",
auth: {
api_user: process.env.USER_SENDMAIL,
api_key: process.env.PASS_SENDMAIL,
}
}
const client = nodemailer.createTransport(sgTransport(options));
const email = result[0].email;
const mailChanged = {
from: "[email protected]",
to: email,
subject: "Ghosted - Password Changed",
html:
"You are receiving this because your password was changed.<br/><br/>" +
"If you did not change your password please change it immediately or contact us on:<br/><br/>"+
"<p style='font-size: 22px; font-weight: bold;'>[email protected]</p><br/><br/><br/>" +
"With your email and your old password. We can only help within 2 hours.<br/><br/>" +
"<p>© Ghosted | 2020</p>",
}
sgMail.send(mailChanged).then(() => {
console.log("Reset email sent!")
res.status(200).send({ response: "Password was updated successfully", statusC: "200"});
})
}
insertPassword();
} else if(result[0].resetPasswordExpires+3600 < currentTime) {
const removeToken = "UPDATE ghosted SET resetPasswordToken=?, resetPasswordExpires=? WHERE email=?;";
const email = result[0].email;
connection.query(removeToken, [null, null, email]);
console.log('Set to NULL');
res.status(403).send({response: "The token is expired.", statusC: "403"});
}
})
})
/* Movie Watchlist */
app.post("/api/bookmarked", (req, res) => {
const bookID = req.body.bookmarkID;
const uid = req.body.uid;
const ins = req.body.bookmarked
if(ins===true) {
const insertTo = "INSERT INTO bookmarks (id, bookmarkID, status) VALUES (?,?,?);";
connection.query(insertTo, [uid, bookID, false], (error) => {
if(error) console.error(error)
res.end();
})
} else if(ins===false){
const deleteFrom = "DELETE FROM bookmarks WHERE id=? AND bookmarkID=?;";
connection.query(deleteFrom, [uid, bookID], (error) => {
if(error) console.error(error);
res.end();
});
}
})
app.post("/api/checkBookmark", (req, res) => {
const checkID = req.body.uid;
const MID = req.body.mid;
const checkDB = "SELECT * FROM bookmarks WHERE id=? AND bookmarkID=?";
console.log(checkID)
connection.query(checkDB, [checkID, MID], (req, results) => {
console.log(results)
if(results.length < 1) {
res.status(200).send({booked: false});
} else {
res.status(200).send({booked: true});
}
})
});
app.post("/api/watchlist", (req, res) => {
const uid = req.body.uid;
const getBookmarked = "SELECT * FROM bookmarks WHERE id=?";
connection.query(getBookmarked, [uid], async (req, resp) => {
let ids = resp.map((i) => ({
id: i.bookmarkID,
statusOf: i.status,
}))
var action = () => {
Promise.all(
ids.map(async(u) => {
let mData = await axios.get('https://api.themoviedb.org/3/movie/'+u.id+'?api_key='+process.env.API_KEY_MOVIES);
let statusOf = u.statusOf;
return [mData.data, statusOf];
})
).then((values) => {
res.send(values)
})
}
action();
})
})
app.post("/api/updateStatus", (req, res) => {
const setStatus = req.body.statusInfo;
const uid = req.body.uid;
const bid = req.body.bid;
const updateStats = "UPDATE bookmarks SET status=? WHERE id=? AND bookmarkID=?";
connection.query(updateStats, [setStatus, uid, bid])
})
/* TV Watchlist */
app.post("/api/bookmarkedTV", (req, res) => {
const tvID = req.body.tvID;
const uid = req.body.uid;
const ins = req.body.bookmarked;
console.log(ins)
if (ins === true) {
const insertTV = "INSERT INTO bookmarksTV (id, tvID, status) VALUES (?,?,?);";
connection.query(insertTV, [uid, tvID, false], (error) => {
if (error) console.error(error)
res.end();
})
} else if(ins===false) {
const removeTV = "DELETE FROM bookmarksTV WHERE id=? AND tvID=?;";
connection.query(removeTV, [uid, tvID], (error) => {
if (error) console.error(error)
res.end();
})
}
})
app.post("/api/checkBookmarkTV", (req, res) => {
const tvID = req.body.tvid;
const uid = req.body.uid;
console.log(tvID, uid)
const checkDB = "SELECT * FROM bookmarksTV WHERE id=? AND tvID=?";
connection.query(checkDB, [uid, tvID], (req, results) => {
console.log(results)
if(results.length < 1) {
res.status(200).send({booked: false});
} else {
res.status(200).send({booked: true});
}
})
});
app.post("/api/watchlistTV", (req, res) => {
const uid = req.body.uid;
const getBookmarkedTV = "SELECT * FROM bookmarksTV WHERE id=?";
connection.query(getBookmarkedTV, [uid], async (req, resp) => {
let ids = resp.map((i) => ({
id: i.tvID,
statusOf: i.status,
}))
var action = () => {
Promise.all(
ids.map(async(u) => {
let mData = await axios.get('https://api.themoviedb.org/3/tv/'+u.id+'?api_key='+process.env.API_KEY_MOVIES);
let statusOf = u.statusOf;
return [mData.data, statusOf];
})
).then((values) => {
res.send(values)
})
}
action();
})
})
app.post("/api/updateStatusTV", (req, res) => {
const setStatus = req.body.statusInfo;
const uid = req.body.uid;
const tid = req.body.tid;
const updateStats = "UPDATE bookmarksTV SET status=? WHERE id=? AND tvID=?";
connection.query(updateStats, [setStatus, uid, tid])
})
app.get("/api/posts", verifyToken, (req, res) => {
jwt.verify(req.token, process.env.ACCESS_TOKEN_SECRET, (err, authData) => {
if (err) {
res.sendStatus(403);
} else {
res.json({
message: "Post created...",
authData: authData,
token: req.token,
});
}
});
});
// Format of token
// Authorization: Bearer <accessToken>
// Verify Token
function verifyToken(req, res, next) {
// Get auth header value
const bearerHeader = req.headers["authorization"];
//Check if bearer is undefined
if (typeof bearerHeader !== "undefined") {
// Split at the space
const bearer = bearerHeader.split(" ");
// Get token from array
const bearerToken = bearer[1];
// Set the token
req.token = bearerToken;
// Next middleware
next();
} else {
// Forbidden
res.sendStatus(403);
}
}
app.listen(3000);