-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
386 lines (315 loc) · 11.8 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import mongoose from "mongoose";
import NodeMailer from "nodemailer";
import * as http from 'http';
import { Server } from "socket.io";
import User from './models/user.js';
import bcrypt from 'bcryptjs';
import * as validator from 'email-validator';
import jsonwebtoken from "jsonwebtoken";
import cookieParser from "cookie-parser";
import { v4 as uuidV4 } from 'uuid';
import * as fs from 'fs';
import fetch from "node-fetch";
import { ExpressPeerServer } from "peer";
import { ResetPassword } from "./models/reset.js";
import { resetMail } from "./mail/ResetPassword.js";
import { ObjectId } from "mongodb";
const app = express();
dotenv.config();
mongoose.connect(process.env.FIREWOOD_DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
});
app.use(cors());
app.use(express.json());
app.use(cookieParser());
const server = http.Server(app);
const io = new Server(server, { allowEIO3: true });
// Peer Server
const peerServer = ExpressPeerServer(server, {
path: '/firewood'
});
app.use('/peerjs', peerServer);
const cookieAge = 3; // in days
app.set('view engine', 'ejs');
app.use(express.static("public"));
app.use('/sounds', express.static("sounds"));
app.use((req, res, next) => {
// Get auth token from the cookies
const token = req.cookies['token'];
// Inject the user to the request
try {
req.user = jsonwebtoken.verify(token, process.env.JWT_SECRET);
} catch (error) {
res.clearCookie('token');
req.user = false;
}
next();
});
const loginRequired = (req, res, next) => {
if (req.user) {
next();
} else {
res.redirect('/');
}
};
app.get('/', (req, res) => {
var token = req.cookies['token'];
try {
var user = jsonwebtoken.verify(token, process.env.JWT_SECRET);
console.log("Current: ", user);
} catch (error) {
res.clearCookie('token');
return res.render('home-no-user');
}
res.render('home', { user: user });
});
/*app.get('/account', loginRequired, async (req, res) => {
var token = jsonwebtoken.verify(req.cookies['token'], process.env.JWT_SECRET)
console.log(token)
const user = await User.findById(token['id']).exec()
console.log(user)
res.render('account', { user: user })
})*/
app.get('/new-cabin', loginRequired, (req, res) => {
const cabinAddress = uuidV4();
res.redirect(`/cabin/${cabinAddress}`);
});
app.get('/cabin/:cabin?', loginRequired, (req, res) => {
if (!req.params.cabin) return res.redirect('/');
res.render('cabin', { cabinAddress: req.params.cabin, userId: jsonwebtoken.verify(req.cookies['token'], process.env.JWT_SECRET)['id'], username: jsonwebtoken.verify(req.cookies['token'], process.env.JWT_SECRET)['username'] });
});
io.on('connection', socket => {
socket.on('join-cabin', (cabinAddress, userId, username) => {
socket.join(cabinAddress);
socket.to(cabinAddress).emit('user-connected', userId, username);
console.log(`${username} (${userId}) connected to '${cabinAddress}'`);
socket.on('data test', (data) => {
console.log(data);
});
socket.on('leave-cabin', () => {
console.log(`${username} (${userId}) disconnected from '${cabinAddress}'`);
socket.to(cabinAddress).emit('user-disconnected', userId, username);
socket.disconnect(true);
});
});
});
app.get('/register', (req, res) => {
var token = req.cookies['token'];
if (token) {
try {
var user = jsonwebtoken.verify(token, process.env.JWT_SECRET);
console.log("Current: ", user);
} catch (error) {
res.clearCookie('token');
res.redirect('/register');
}
return res.redirect('/');
}
return res.render('register');
});
app.get('/login', (req, res) => {
let response = fetch('https://4d4e-122-161-90-135.in.ngrok.io/api/state.json');
let data = response;
console.log(data);
var token = req.cookies['token'];
if (token) {
try {
var user = jsonwebtoken.verify(token, process.env.JWT_SECRET);
console.log("Current: ", user);
} catch (error) {
res.clearCookie('token');
res.redirect('/login');
}
return res.redirect('/');
}
return res.render('login');
});
app.get('/forgot-password', async (req, res) => {
return res.render('password/forgot-password');
});
app.get('/forgot-password/reset-:resetId?', async (req, res) => {
if (!req.params.resetId) return res.redirect('/forgot-password');
var resetId = req.params.resetId;
console.log(resetId);
const reset = await ResetPassword.findOne({ resetURL: resetId }).exec();
if (!reset) return res.redirect('/forgot-password');
console.log(reset);
const user = await User.findById(reset['userId']);
if (!user) return res.redirect('/forgot-password');
return res.render("password/change-password", { id: user['_id'], resetURL: resetId });
});
app.post('/api/change-password', async (req, res) => {
if (!req.body.password || !req.body.confirmation || !req.body.userId || !req.body.resetURL) return res.json({ status: 'error', error: 'Missing fields.' });
if (req.body.password != req.body.confirmation) return res.json({ status: 'error', error: 'Both password fields don\'t match.' });
const password = req.body.password;
if (!(password.length >= 7)) {
return res.json({ status: 'error', error: 'Password is too small. Should be atleast 7 characters.' });
}
const reset = await ResetPassword.findOne({ resetURL: req.body.resetURL, userId: req.body.userId }).exec();
if (!reset) return res.redirect('/forgot-password');
await ResetPassword.deleteOne({ resetURL: req.body.resetURL });
const userId = req.body.userId;
// Finding user
const user = User.findById(req.body.userId).then(
user => {
if (!user) return res.json({ status: 'error', error: "Problem with the reset. Please try again or contact the developer." });
if (user.password == password) return res.json({ status: 'error', error: "This is already your password!" });
user.password = password;
user.save();
console.log(user);
}
).catch(err => console.log(err));
const deleteOldResetLinks = await ResetPassword.deleteMany({ userId: userId });
console.log(deleteOldResetLinks);
return res.json({ status: 'ok' });
});
app.get('/logout', (req, res) => {
res.clearCookie('token');
res.redirect('/');
});
app.post('/api/login', async (req, res) => {
var { usermail, password: passwordPlain } = req.body;
var user = null;
if (!usermail || !passwordPlain) {
return res.json({ status: 'error', error: 'Missing fields.' });
}
if (validator.validate(usermail)) {
// input is an e-mail
usermail = usermail.toLowerCase();
console.log(usermail);
if (typeof (usermail) !== 'string') {
return res.json({ status: 'error', error: 'Invalid e-mail/username/password' });
}
user = await User.findOne({ email: usermail }).exec();
}
else {
usermail = usermail;
console.log(usermail);
user = await User.findOne({ username: usermail }).exec();
}
if (!user) {
return res.json({ status: 'error', error: 'Invalid e-mail/username/password' });
}
console.log(user);
var hash = user['password'];
console.log(hash);
if (await bcrypt.compare(passwordPlain, hash)) {
// Found
console.log('Logged in.');
const token = jsonwebtoken.sign({ id: user._id, username: user.username }, process.env.JWT_SECRET);
res.cookie('token', token, { maxAge: 1000 * 60 * 60 * 24 * cookieAge });
return res.json({ status: 'ok', data: token });
}
return res.json({ status: "error", error: 'Invalid e-mail/username/password' });
});
app.post('/api/forgot-password', async (req, res) => {
if (!req.body.usermail) return res.json({ status: 'error', error: 'No username/e-mail provided' });
var usermail = req.body.usermail;
var user;
if (validator.validate(usermail)) {
// input is an e-mail
usermail = usermail.toLowerCase();
console.log(usermail);
if (typeof (usermail) !== 'string') {
return res.json({ status: 'error', error: 'Invalid e-mail/username' });
}
user = await User.findOne({ email: usermail }).exec();
}
else {
usermail = usermail;
console.log(usermail);
user = await User.findOne({ username: usermail }).exec();
}
if (!user) {
return res.json({ status: 'error', error: 'Invalid e-mail/username' });
}
const userId = user['_id'].valueOf();
const resetURL = `${uuidV4()}`;
const response = await ResetPassword.create({
userId,
resetURL
});
console.log(response);
var emailId = response['_id'].valueOf();
const transporter = NodeMailer.createTransport({
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: `${process.env.MAIL}`,
pass: `${process.env.PASSWORD}`
},
});
let mail = await transporter.sendMail({
from: '"Firewood [no-reply]" <[email protected]>',
to: user['email'],
subject: `Firewood: Reset account password - #${emailId}`,
html: resetMail(user['username'], resetURL, emailId)
}).catch(err => console.log(err));
res.json({ status: 'ok' });
});
app.post('/api/register', async (req, res) => {
console.log(req.body);
if (!req.body.username || !req.body.email || !req.body.password || !req.body.confirmation) {
return res.json({ status: 'error', error: 'Missing fields.' });
}
var { username, email, password, confirmation } = req.body;
email = email.toLowerCase();
if (typeof (username) !== 'string') {
return res.json({ status: 'error', error: 'Invalid username.' });
}
if (!validator.validate(email)) {
return res.json({ status: 'error', error: 'Invalid E-mail' });
}
if (!(password.length >= 7)) {
return res.json({ status: 'error', error: 'Password is too small. Should be atleast 7 characters.' });
}
if (password != confirmation) {
return res.json({ status: 'error', error: "Passwords don't match." });
}
var response;
try {
response = await User.create({
username,
email,
password
});
console.log("User created successfully " + response);
} catch (error) {
if (error.code === 11000) {
// duplicate username or e-mail
return res.json({ status: 'error', error: 'Username/E-mail already in use.' });
}
throw error;
}
const token = jsonwebtoken.sign({ id: response._id, username: response.username }, process.env.JWT_SECRET);
res.cookie('token', token, { maxAge: 1000 * 60 * 60 * 24 * cookieAge });
res.json({ status: 'ok' });
});
app.get('/api/state', (req, res) => {
res.type('json');
var state = null;
var token = req.cookies['token'];
if (token) {
try {
var user = jsonwebtoken.verify(token, process.env.JWT_SECRET);
state = "logged-in";
return res.send(JSON.stringify({ state: state, username: user['username'] }));
} catch (error) {
state = "error";
res.clearCookie();
return res.send(JSON.stringify({ state: state, username: null }));
}
}
else {
state = null;
return res.send(JSON.stringify({ state: state, username: null }));
}
});
const listener = server.listen(process.env.PORT || 3000, () => {
console.log(`Listening on ${listener.address().port}`);
});