This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.js
199 lines (179 loc) · 5.37 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
const express = require('express');
const bcrypt = require('bcrypt');
const app = express();
const cors = require('cors');
let jwt = require('jsonwebtoken');
let config = require('./config');
let middleware = require('./middleware');
const bp = require('body-parser');
const db = require('./db');
const socket = require('socket.io');
const { response } = require('express');
app.use(bp.urlencoded({ extended: true }));
app.use(cors());
app.use(express.json());
app.get('/', (req, res) => {
res.json('Hi');
});
const users = [];
const addUser = ({ id, name, room }) => {
name = name.trim().toLowerCase();
room = room.trim().toLowerCase();
const existingUser = users.find((user) => user.room === room && user.name === name);
if (!name || !room) return { error: 'Username and room are required.' };
if (existingUser) return { error: 'Username is taken.' };
const user = { id, name, room };
users.push(user);
return { user };
};
const removeUser = (id) => {
const index = users.findIndex((user) => user.id === id);
if (index !== -1) return users.splice(index, 1)[0];
};
const getUser = (id) => users.find((user) => user.id === id);
const getUsersInRoom = (room) => users.filter((user) => user.room === room);
app.post('/register', (req, res) => {
const { email, username, password } = req.body;
const hash = bcrypt.hashSync(password, 10);
db.query(
'INSERT INTO users(username,email,password) VALUES($1,$2,$3)',
[ username, email, hash ],
(err, response) => {
console.log(err, response);
}
);
res.json('success');
});
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = 'SELECT password FROM login WHERE "username" = $1';
db.query(query, [ username ], (err, response) => {
if (err) {
console.log(err.stack);
} else {
if (!response.rows[0]) {
res.json('incorrect username');
} else {
const hash = response.rows[0].password;
bcrypt.compare(password, hash, function(err, result) {
if (err) {
console.log(err);
}
if (result == true) {
let token = jwt.sign({ username: username }, config.secret, { expiresIn: '24h' });
res.json({
success: true,
message: 'Authentication successful!',
token: token
});
} else {
res.json('incorrect password');
}
});
}
}
});
});
app.post('/room', middleware.checkToken, (req, res) => {
const { roomName, creater, password } = req.body;
const hash = bcrypt.hashSync(password, 10);
db.query(
'INSERT INTO rooms(roomname,creater,password) VALUES ($1,$2,$3)',
[ roomName, creater, hash ],
(err, response) => {
console.log(err, response);
if (err) {
res.json('Fail');
}
res.json('Success');
}
);
});
app.post('/join', middleware.checkToken, (req, res) => {
const { roomName, password, token } = req.body;
const query = 'SELECT password FROM rooms WHERE "roomname" = $1';
db.query(query, [ roomName ], (err, response) => {
if (err) {
console.log(err.stack);
} else {
if (!response.rows[0]) {
res.json('incorrect roomName');
} else {
const hash = response.rows[0].password;
bcrypt.compare(password, hash, function(err, result) {
if (err) {
console.log(err);
}
if (result == true) {
res.json('entry successfull');
} else {
res.json('incorrect password');
}
});
}
}
});
});
app.post('/list', (req, res) => {
const { username } = req.body;
console.log(username);
db.query('SELECT username,roomname FROM joined WHERE "username"=$1', [ username ], (err, response) => {
if (err) {
console.log(err);
}
res.json(response.rows);
});
});
app.post('/chat', middleware.checkToken, (req, res) => {
const { room } = req.body;
db.query('SELECT user,text FROM chat WHERE "room"=$1', [ room ], (err, response) => {
if (err) {
console.log(err);
}
res.json(response.rows);
});
});
app.post('/chatCheck', middleware.checkToken, (req, res) => {
res.json('Authorized');
});
app.post('/dash', (req, res) => {
const { username } = req.body;
db.query('SELECT userid,username,email FROM users WHERE "username"=$1', [ username ], (err, response) => {
if (err) {
console.log(err);
}
res.json(response.rows[0]);
});
});
app.get('/logout', (req, res) => {
console.log('logged out');
});
const server = app.listen(process.env.PORT || 3001, () => {
console.log(`App is running on ${process.env.PORT}`);
});
var data = [];
const io = socket(server);
io.on('connection', (socket) => {
console.log('connection', socket.id);
socket.on('join', ({ name, room }, callback) => {
const { error, user } = addUser({ id: socket.id, name, room });
if (error) return callback(error);
socket.join(user.room);
socket.emit('message', { user: 'admin', text: `${user.name}, welcome to room ${user.room}.` });
socket.broadcast.to(user.room).emit('message', { user: 'admin', text: `${user.name} has joined!` });
io.to(user.room).emit('roomData', { room: user.room, users: getUsersInRoom(user.room) });
callback();
});
socket.on('sendMessage', (message, callback) => {
const user = getUser(socket.id);
io.to(user.room).emit('message', { user: user.name, text: message });
callback();
});
socket.on('disconnect', () => {
const user = removeUser(socket.id);
if (user) {
io.to(user.room).emit('message', { user: 'Admin', text: `${user.name} has left.` });
io.to(user.room).emit('roomData', { room: user.room, users: getUsersInRoom(user.room) });
}
});
});