-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
391 lines (306 loc) · 9.5 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
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const mongoose = require('mongoose')
const User = require('./model/user')
const event = require('./model/event')
const Organizer = require('./model/organizer')
const Applied = require('./model/applied')
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const { events } = require('./model/user')
const response = require('./model/response')
const JWT_SECRET = 'sdjkfh8923yhjdksbfma@#*(&@*!^#&@bhjb2qiuhesdbhjdsfg839ujkdhfjk'
mongoose.connect('mongodb://localhost:27017/loginapp', {
useNewUrlParser: true,
useUnifiedTopology: true,
})
const app = express()
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
console.log('A user connected');
socket.on('disconnect', function(){
console.log('A user disconnected');
});
socket.on('chat message', function(author, message){
io.emit('chat message', author, message);
});
});
app.use('/', express.static(path.join(__dirname, 'static')))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}));
app.set('view engine', 'ejs');
app.post('/api/change-password', async (req, res) => {
const { token, newpassword: plainTextPassword } = req.body
if (!plainTextPassword || typeof plainTextPassword !== 'string') {
return res.json({ status: 'error', error: 'Invalid password' })
}
if (plainTextPassword.length < 5) {
return res.json({
status: 'error',
error: 'Password too small. Should be atleast 6 characters'
})
}
try {
const user = jwt.verify(token, JWT_SECRET)
const _id = user.id
const password = await bcrypt.hash(plainTextPassword, 10)
await User.updateOne(
{ _id },
{
$set: { password }
}
)
res.json({ status: 'ok' })
} catch (error) {
console.log(error)
res.json({ status: 'error', error: ';))' })
}
})
app.post('/api/login', async (req, res) => {
const { username, password } = req.body
const user = await User.findOne({ username }).lean()
if (!user) {
return res.json({ status: 'error', error: 'Invalid username/password' })
}
if (await bcrypt.compare(password, user.password)) {
// the username, password combination is successful
const token = jwt.sign(
{
id: user._id,
username: user.username
},
JWT_SECRET
)
return res.json({ status: 'ok', data: token })
}
res.render('home.ejs');
res.json({ status: 'error', error: 'Invalid username/password' })
})
let organizerUsername;
app.post('/api/ologin', async (req, res) => {
const { username, password } = req.body
organizerUsername = username;
console.log(organizerUsername);
const organizer = await Organizer.findOne({ username }).lean()
if (!organizer) {
return res.json({ status: 'error', error: 'Invalid username/password' })
}
if (await bcrypt.compare(password, organizer.password)) {
// the username, password combination is successful
const token = jwt.sign(
{
id: organizer._id,
username: organizer.username
},
JWT_SECRET
)
return res.json({ status: 'ok', data: token })
}
res.render('home.ejs');
res.json({ status: 'error', error: 'Invalid username/password' })
})
app.post('/api/register', async (req, res) => {
const { username, password: plainTextPassword } = req.body
if (!username || typeof username !== 'string') {
return res.json({ status: 'error', error: 'Invalid username' })
}
if (!plainTextPassword || typeof plainTextPassword !== 'string') {
return res.json({ status: 'error', error: 'Invalid password' })
}
if (plainTextPassword.length < 5) {
return res.json({
status: 'error',
error: 'Password too small. Should be atleast 6 characters'
})
}
const password = await bcrypt.hash(plainTextPassword, 10)
try {
const response = await User.create({
username,
password
})
console.log('User created successfully: ', response)
} catch (error) {
if (error.code === 11000) {
// duplicate key
return res.json({ status: 'error', error: 'Username already in use' })
}
throw error
}
res.json({ status: 'ok' })
})
app.post('/api/oregister', async (req, res) => {
const { username, password: plainTextPassword } = req.body
if (!username || typeof username !== 'string') {
return res.json({ status: 'error', error: 'Invalid username' })
}
if (!plainTextPassword || typeof plainTextPassword !== 'string') {
return res.json({ status: 'error', error: 'Invalid password' })
}
if (plainTextPassword.length < 5) {
return res.json({
status: 'error',
error: 'Password too small. Should be atleast 6 characters'
})
}
const password = await bcrypt.hash(plainTextPassword, 10)
try {
const response = await Organizer.create({
username,
password
})
console.log('Organizer created successfully: ', response)
} catch (error) {
if (error.code === 11000) {
// duplicate key
return res.json({ status: 'error', error: 'Username already in use' })
}
throw error
}
res.json({ status: 'ok' })
})
app.get('/register.html', function(req, res){
res.sendFile(__dirname + '/register.html');
});
app.get('/index.html', function(req, res){
res.sendFile(__dirname + '/index.html');
})
app.get('/login.html', function(req, res){
res.sendFile(__dirname + '/login.html');
})
app.get('/ologin.html', function(req, res){
res.sendFile(__dirname + '/ologin.html');
})
app.get('/', function(req, res){
res.sendFile(__dirname + '/login.html');
});
app.get('/ohome.html', function(req, res){
res.sendFile(__dirname + '/ohome.html');
});
app.get('/home.html', function(req, res){
res.sendFile(__dirname + '/home.html');
});
app.get('/postEvent.html', function(req, res){
res.sendFile(__dirname + '/postEvent.html');
})
app.post('/postEvent.html', async function(req, res){
console.log(req.body)
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/loginapp";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("loginapp");
var myobj = { username: req.body.name, email: req.body.email, phone: req.body.phone, eventname: req.body.eventname, info: req.body.info };
console.log(req.body.eventname);
dbo.collection("events").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 event inserted");
db.close();
});
});
res.sendFile(__dirname + "/home.html")
});
app.get("/trending.html", function(req, res){
res.sendFile(__dirname + '/trending.html');
});
app.get("/checkEvents.html", function(req, res){
// res.sendFile(__dirname + "/checkEvents.html");
event.find({}, function(err, docs){
if(err) res.json(err)
else res.render("checkEvents", {data: docs});
})
});
app.get("/appliedEvents.html", function(req, res){
Applied.find({name: organizerUsername}, function(err, docs){
if(err) res.json(err)
else res.render("appliedEvents", {data: docs})
})
})
app.get("/myevents.html", function(req, res){
Applied.find({}, function(err, docs){
if(err) res.json(err)
else res.render("myevents", {data: docs})
})
})
app.get("/inter.html", function(req, res){
res.sendFile(__dirname + "/inter.html")
})
let eventN;
app.post("/inter.html", function(req, res){
eventN = req.body.eventName;
console.log(eventN);
Applied.find({eventname: eventN}, function(err, docs){
if(err) res.json(err)
else res.render("myEvents", {data: docs})
})
})
app.get("/appliedEvents.html", function(req, res){
res.sendFile(__dirname + "/appliedEvents.html");
});
app.get("/oevent.html", function(req, res){
res.render("oevent");
});
app.get("/ohome.html", function(req, res){
res.render("ohome");
});
app.post("/postoEvent.html", function(req, res){
console.log(req.body);
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/loginapp";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("loginapp");
var myobj = { name: req.body.name, eventname: req.body.eventname, response: req.body.response, price: req.body.price, email: req.body.email};
console.log(req.body.eventname);
dbo.collection("response").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("1 response inserted");
db.close();
});
});
res.render("ohome.ejs");
});
app.get('/pune.html', function(req, res){
res.sendFile(__dirname + '/pune.html');
});
app.get('/delhi.html', function(req, res){
res.sendFile(__dirname + '/delhi.html');
});
app.get('/mumbai.html', function(req, res){
res.sendFile(__dirname + '/mumbai.html');
});
app.get('/bangalore.html', function(req, res){
res.sendFile(__dirname + '/bangalore.html');
})
// app.get('/token', (req, res) => {
// const { username } = req.query
// if (username) {
// const token = serverClient.createToken(username)
// res.status(200).json({ token, status: "sucess" })
// } else {
// res.status(401).json({ message: "invalid request", status: "error" })
// }
// });
// app.post('/updateUser', async (req, res) => {
// const { userID } = req.body
// if (userID) {
// const updateResponse = await serverClient.updateUsers([{
// id: userID,
// role: 'admin'
// }]);
// res.status(200).json({ user: updateResponse, status: "sucess" })
// } else {
// res.status(401).json({ message: "invalid request", status: "error" })
// }
// });
// app.get('/chat.html', (req, res) => {
// window.open("https://mail.google.com/mail/u/0/#inbox", "Mail", "width=500 height=300");
// });
app.listen(9999, () => {
console.log('Server up at 9999')
});
// XwwZiarbRWJQIOmOADG-YHwtmlGHJq1Uo-PGdu5F