-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
384 lines (316 loc) · 11.7 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
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
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const path = require('path');
const http = require('http');
const socketio = require('socket.io');
const sqlite3 = require('sqlite3').verbose();
const nodemailer = require('nodemailer');
var favicon = require('serve-favicon');
const port = process.env.PORT || 3000;
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail', // use 'gmail' as an example, you can use other services
auth: {
user: '[email protected]', // your email
pass: 'dont izuz iggb jkme' // your email password
}
});
// Cookie Configuration Start
app.use(cookieParser());
// Cookie Configuration End
// Database Configuration Start
const db = new sqlite3.Database('fluxxy.db');
db.serialize(() => {
console.log('Database Connected Successfully!\n-> fluxxy.db\n');
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Database Configuration End
app.use(express.static('public'));
// Favicon Start
app.use(favicon(path.join(__dirname, 'public', 'assets', 'images', 'logos', 'logo.png')));
// Favicon End
app.get('/', (req, res) => {
if (req.cookies.email) {
res.redirect('/chat');
} else {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
});
app.get('/login', function(req, res) {
if (req.cookies.email) {
res.redirect('/chat');
} else {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
}
});
app.get('/register', function(req, res) {
if (req.cookies.email) {
res.redirect('/chat');
} else {
res.sendFile(path.join(__dirname, 'public', 'register.html'));
}
});
app.get('/chat', function(req, res) {
if (req.cookies.email) {
res.sendFile(path.join(__dirname, 'public', 'chat.html'));
} else {
res.redirect('/login');
}
});
let onlineCount = 0;
io.on('connection', (socket) => {
console.log('WebSocket Connected :D');
onlineCount++;
io.emit('onlineCount', onlineCount);
socket.on('chatMessage', (msg) => {
io.emit('chatMessage', msg);
});
socket.on('disconnect', () => {
onlineCount--;
io.emit('onlineCount', onlineCount);
console.log('WebSocket Disconnected :O\nOnline Users:', onlineCount);
});
console.log('Online Users:', onlineCount);
});
// Register User Start
app.post('/userRegister', (req, res) => {
const name = req.body.name;
const email = req.body.email;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;
const timestamp = new Date().getDate() + '/' + (new Date().getMonth() + 1) + '/' + new Date().getFullYear() + ' ' + new Date().toTimeString().slice(0, 5);
if (password === confirmPassword) {
// Check if email already exists in the database
db.get('SELECT email FROM users WHERE email = ?', [email], (err, row) => {
if (err) {
console.error('Error querying database:', err.message);
res.status(500).send('Error querying database :(');
return;
}
if (row) {
// If the SELECT statement returned a row, the email already exists
console.error('Email already exists');
res.status(500).send(`<script>alert('Email already exists'); window.history.back();</script>`);
return;
}
// If the SELECT statement didn't return a row, the email doesn't exist and we can insert the new user
db.run('INSERT INTO users (name, email, password, timestamp) VALUES (?, ?, ?, ?)', [name, email, password, timestamp], (err) => {
if (err) {
console.error('Error Saving Data To Database:', err.message);
res.status(500).send('Error Saving Data To Database :(');
return;
}
console.log('Data Saved To Database :)');
// HTML To Display Thanks & Submitted Data To User
let userRegisterHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#2695D4">
<title>Welcome - Fluxxy</title>
<!-- SEO Start =================================================================================-->
<meta name="title" content="Welcome - Fluxxy">
<meta name="description" content="Fluxxy is an open source website to share texts and many more with the people around the world, no matter where you are.">
<!-- Open Graph -->
<meta property="og:type" content="website">
<meta property="og:url" content="">
<meta property="og:title" content="Welcome - Fluxxy">
<meta property="og:description" content="Fluxxy is an open source website to share texts and many more with the people around the world, no matter where you are.">
<meta property="og:image" content="https://i.postimg.cc/1XbsDBhv/preview.png">
<!-- Twitter -->
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:url" content="">
<meta name="twitter:title" content="Welcome - Fluxxy">
<meta name="twitter:description" content="Fluxxy is an open source website to share texts and many more with the people around the world, no matter where you are.">
<meta name="twitter:image" content="https://i.postimg.cc/1XbsDBhv/preview.png">
<!--=================================================================================== SEO End -->
<meta name="apple-mobile-web-app-status-bar" content="#2695D4">
<link rel="apple-touch-icon" href="https://i.postimg.cc/mZJ0wS0v/logo.png">
<link rel="manifest" href="/manifest.json">
<script src="/service-register.js"></script>
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
</head>
<body>
<h1>Welcome</span> ${name.split(' ')[0]}!</h1>
<p>You're just one step away from accessing all the features of Fluxxy. Login to your account to get started.</p>
<button style="background: var(--accent);" onclick="window.location.href='/login'">Login Now</button>
</body>
</html>
`;
res.send(userRegisterHtml);
// after user is registered
let mailOptions = {
from: '[email protected]', // sender address
to: email, // list of receivers
subject: 'Welcome to Fluxxy', // Subject line
text: 'Thank you for registering ' + name.split(' ')[0] + '.', // plain text body
html: `
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
color: #333;
}
a {
color: #4CAF50;
text-decoration: none;
}
a:hover {
color: #3e8e41;
}
.container {
padding: 20px;
max-width: 600px;
margin: 0 auto;
border-radius: 5px;
background-color: #f2f2f2;
}
.header {
text-align: center;
}
.content {
padding: 20px;
}
.footer {
text-align: center;
padding: 10px;
background-color: #ddd;
}
</style>
<div class="container">
<div class="header">
<h1>Welcome to Fluxxy, ${name.split(' ')[0]}!</h1>
</div>
<div class="content">
<p>Thank you for registering with Fluxxy. We're excited to have you on board!</p>
<p>We're constantly adding new features for you to try, so be sure to check back often!</p>
<h2>Getting Started</h2>
<p>Ready to get started? Click the button below to login to your account:</p>
<a href="https://fluxxy.onrender.com/login" style="background-color: #4CAF50; color: white; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; border-radius: 5px;">Login Now</a>
<p>If you have any questions or suggestions, please don't hesitate to contact us at <a href="mailto:[email protected]">[email protected]</a>.</p>
<p>We're here to support you in any way we can.</p>
<p>Warmly,</p>
<p>Fluxxy Team</p>
</div>
<div class="footer">
<p>© 2024 Fluxxy</p>
</div>
</div>
</body>
</html>
` // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
});
});
});
} else {
console.error('Password and Confirm Password do not match');
res.status(500).send(`<script>alert('Password and Confirm Password do not match'); window.history.back();</script>`);
return;
}
});
// Register User End
// Login User Start
app.post('/userLogin', (req, res) => {
const email = req.body.email;
const password = req.body.password;
db.get('SELECT * FROM users WHERE email = ?', [email], (err, row) => {
if (err) {
console.error(err.message);
res.status(500).send('Error Fetching Data From Database :(');
return;
}
if (row) {
if (password === row.password) {
console.log('Login Successful :)');
res.cookie('userid', row.id, {
maxAge: 1000 * 60 * 60 * 24 * 30, // Expires in 30 days
httpOnly: true, // Only accessible by the server
secure: false // Only sent over HTTPS
});
console.log('User ID Cookie Set Successfully');
res.cookie('email', email, {
maxAge: 1000 * 60 * 60 * 24 * 30, // Expires in 30 days
httpOnly: true, // Only accessible by the server
secure: false // Only sent over HTTPS
});
console.log('Email Cookie Set Successfully');
res.cookie('name', row.name, { // Save the name of the user from the database
maxAge: 1000 * 60 * 60 * 24 * 30, // Expires in 30 days
httpOnly: true, // Only accessible by the server
secure: false // Only sent over HTTPS
});
console.log('Name Cookie Set Successfully');
res.redirect('/chat');
} else {
console.error('Invalid Password :(');
res.status(500).send(`<script>alert('Invalid Password :('); window.history.back();</script>`);
}
} else {
console.error('Invalid Email :(');
res.status(500).send(`<script>alert('Invalid Email :('); window.history.back();</script>`);
}
});
});
// Login User End
// Loggedin User Info Endpoint Start
app.get('/api/loggedin-userinfo', (req, res) => {
const userInfo = {
userid: req.cookies.userid,
email: req.cookies.email,
name: req.cookies.name
};
res.json(userInfo);
});
// Loggedin User Info Endpoint End
// Logout User Start
app.get('/logout', (req, res) => {
function removeCookies() {
res.clearCookie('userid');
console.log('User ID Cookie Cleared');
res.clearCookie('email');
console.log('Email Cookie Cleared');
res.clearCookie('name');
console.log('Name Cookie Cleared');
}
removeCookies();
res.redirect('/login');
});
// Logout User End
// Serve the service worker file
app.get('/service-worker.js', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'service-worker.js'));
});
// Serve the manifest file
app.get('/manifest.json', (req, res) => {
res.sendFile(path.resolve(__dirname, 'public', 'manifest.json'));
});
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, 'public', '404.html'));
});
server.listen(port, () => {
console.log(`Server listening at\n-> http://localhost:${port}\n`);
});