-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
373 lines (315 loc) · 11.2 KB
/
server.js
File metadata and controls
373 lines (315 loc) · 11.2 KB
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
const express = require("express"); //web framework for Node.JS
const bcrypt = require("bcryptjs"); //password hashing library, Converts a plain-text password into a hashed version that's difficult to reverse-engineer.
const jwt = require("jsonwebtoken"); //JSON web token library
const { body, validationResult } = require("express-validator"); //validation library
const dotenv = require("dotenv"); //load environmental variables
const cors = require("cors"); //enable cross origin resource sharing: TODO??
const path = require('path');
const { authenticateToken } = require('./src/middleware/auth');
const socketService = require('./src/services/socketService');
//import local modules:
const db = require('./src/config/db'); //database connection
const incidentRoutes = require('./src/routes/incidents');
const profileRoutes = require('./src/routes/profile');
const chatsRouter = require('./src/routes/chats');
const newsRouter = require('./src/routes/news');
//http is a protocol that allows the exchange of data between a client and a server
const http = require('http');
//socket.io is a library that enables real-time, bidirectional communication between the browser and the server
const { Server } = require('socket.io');
const { checkRedisHealth } = require('./src/utils/healthCheck');
// Load environment variables
dotenv.config();
//creates an instance of an express application`
const app = express();
//creates an HTTP server that wraps the express application
const server = http.createServer(app);
//creates a new instance of the Socket.IO server, passing in the HTTP server as the argument
const io = new Server(server, {
//cors is a middleware that allows cross-origin requests
cors: {
origin: [
"http://localhost:9000",
"http://localhost:3000",
"http://0.0.0.0:9000",
"http://127.0.0.1:9000",
"http://localhost:8080",
"http://localhost",
"https://*.herokuapp.com"
],
methods: ["GET", "POST"],
credentials: true
}
});
// Middleware for http requests
app.use(cors({
origin: [
"http://localhost:9000",
"http://localhost:3000",
"http://0.0.0.0:9000",
"http://127.0.0.1:9000",
"http://localhost:8080",
"http://localhost",
"https://*.herokuapp.com"
],
credentials: true
}));
app.use(express.json());
//why 2 different cors?
//1. cors is used for http requests
//2. socket.io is used for websocket connections
//check health of Redis and Database
app.get('/api/health', async (req, res) => {
try {
// Basic app health check
const appHealth = true;
// Database health check
let dbHealth = false;
try {
await db.query('SELECT 1');
dbHealth = true;
} catch (error) {
console.error('Database health check failed:', error);
}
// Redis health check
const redisHealth = await checkRedisHealth();
const isHealthy = appHealth && dbHealth && redisHealth;
res.status(isHealthy ? 200 : 500).json({
status: 'healthy',
database: dbHealth ? 'connected' : 'disconnected',
redis: redisHealth ? 'connected' : 'disconnected'
});
} catch (error) {
console.error('Health check failed:', error);
res.status(500).json({ status: 'unhealthy', error: error.message });
}
});
app.post("/api/register", [
body("email").isEmail().withMessage("Invalid email address"),
body("password")
.isLength({ min: 6 })
.withMessage("Password must be at least 6 characters long"),
], async (req, res) => {
try {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { email, password } = req.body;
console.log('Received registration request for:', email);
// Validate input
if (!email || !password) {
console.log('Missing email or password');
return res.status(400).json({ error: 'Email and password are required' });
}
// Check if user exists
const userExists = await db.query(
'SELECT * FROM users WHERE email = $1',
[email]
);
if (userExists.rows.length > 0) {
console.log('User already exists:', email);
return res.status(400).json({ error: 'User already exists' });
}
// Hash password
console.log('Hashing password...');
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user
console.log('Inserting new user...');
const result = await db.query(
'INSERT INTO users (email, password_hash) VALUES ($1, $2) RETURNING id, email',
[email, hashedPassword]
);
console.log('User inserted:', result.rows[0]);
// Generate token
console.log('Generating JWT token...');
const token = jwt.sign(
{ userId: result.rows[0].id, email: result.rows[0].email },
process.env.JWT_SECRET
);
console.log('Registration successful, sending response');
res.status(201).json({
token,
user: { id: result.rows[0].id, email: result.rows[0].email }
});
} catch (error) {
console.error('Server registration error:', error);
res.status(500).json({
error: 'Failed to register',
details: process.env.NODE_ENV === 'development' ? error.message : undefined,
stack: process.env.NODE_ENV === 'development' ? error.stack : undefined
});
}
});
app.post("/api/login", async (req, res) => {
try {
const { email, password } = req.body;
// Input validation
if (!email || !password) {
return res.status(400).json({ message: 'Email and password are required' });
}
// Get user from database
const result = await db.query(
'SELECT id, email, password_hash FROM users WHERE email = $1',
[email]
);
const user = result.rows[0];
if (!user) {
return res.status(401).json({ message: 'Invalid credentials' });
}
// Compare password
const isValid = await bcrypt.compare(password, user.password_hash);
if (!isValid) {
return res.status(401).json({ message: 'Invalid credentials' });
}
// Generate JWT
const token = jwt.sign(
{ userId: user.id, email: user.email },
process.env.JWT_SECRET,
{ expiresIn: '24h' }
);
console.log('Generated token:', token); // Debug log
res.json({ token, user: { id: user.id, email: user.email } });
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ message: 'Server error: ' + error.message });
}
});
// Add this route near your other authentication routes
app.get('/api/verify-token', authenticateToken, (req, res) => {
res.json({
valid: true,
user: {
id: req.user.id,
email: req.user.email
}
});
});
// Add routes
app.use('/api/incidents', incidentRoutes);
app.use('/api/profile', profileRoutes);
app.use('/api/news', newsRouter);
app.use('/api/chats', chatsRouter);
// Serve static files in production ****!!!!!
if (process.env.NODE_ENV === 'production') {
app.use(express.static('public'));
//catch all route for react router
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
}
//accepts a socket instance as an argument
const setupSocketHandlers = (io) => {
//set up event listener for new socket connections:
io.on('connection', (socket) => {
console.log('Client connected:', socket.id);
// Authenticate socket connection
const token = socket.handshake.auth.token || socket.handshake.query.token;
if (!token) {
console.log('No token provided for socket connection');
socket.disconnect();
return;
}
try {
// Verify and decode the token
const decoded = jwt.verify(token, process.env.JWT_SECRET);
socket.userId = decoded.userId;
socket.userEmail = decoded.email;
console.log('Socket authenticated for user:', decoded.email);
// Join a room specific to this user
socket.join(`user_${decoded.userId}`);
// Handle initial friend locations request
socket.on('get-friend-locations', async () => {
try {
const result = await db.query(`
SELECT DISTINCT
u.id,
u.email,
ST_X(u.last_location::geometry) as longitude,
ST_Y(u.last_location::geometry) as latitude,
u.last_location_timestamp,
u.is_location_enabled
FROM users u
INNER JOIN friends f ON
(f.friend_id = u.id OR f.user_id = u.id)
WHERE (f.user_id = $1 OR f.friend_id = $1)
AND u.id != $1
AND u.is_location_enabled = true
AND u.last_location IS NOT NULL`,
[socket.userId]
);
console.log('Friend locations fetched:', result.rows);
socket.emit('friend-locations', result.rows);
} catch (error) {
console.error('Error fetching friend locations:', error);
socket.emit('error', 'Failed to fetch friend locations');
}
});
// Handle location updates
socket.on('update-location', async (location) => {
try {
// Update user's location in database
await db.query(`
UPDATE users
SET last_location = ST_SetSRID(ST_MakePoint($1, $2), 4326),
last_location_timestamp = CURRENT_TIMESTAMP
WHERE id = $3 AND is_location_enabled = true`,
[location.longitude, location.latitude, socket.userId]
);
// Fetch and notify friends who have location enabled
const friendsResult = await db.query(`
SELECT DISTINCT
CASE
WHEN f.user_id = $1 THEN f.friend_id
ELSE f.user_id
END as friend_id
FROM friends f
INNER JOIN users u ON
(CASE WHEN f.user_id = $1 THEN f.friend_id ELSE f.user_id END) = u.id
WHERE (f.user_id = $1 OR f.friend_id = $1)
AND u.is_location_enabled = true`,
[socket.userId]
);
// Emit location update to all friends
friendsResult.rows.forEach(friend => {
io.to(`user_${friend.friend_id}`).emit('friend-location-update', {
userId: socket.userId,
email: socket.userEmail,
latitude: location.latitude,
longitude: location.longitude,
timestamp: new Date()
});
});
} catch (error) {
console.error('Error updating location:', error);
socket.emit('error', 'Failed to update location');
}
});
// Handle disconnection
socket.on('disconnect', () => {
console.log('Client disconnected:', socket.userEmail);
});
} catch (error) {
console.error('Socket authentication error:', error);
socket.disconnect();
}
});
};
// Set up socket handlers
setupSocketHandlers(io);
socketService.init(io);
//Setting up port so that the server can listen for incoming requests
const PORT = process.env.PORT || 3000;
server.listen(PORT, '0.0.0.0', (err) => {
if (err) {
console.error('Error starting server:', err);
return;
}
console.log(`Server running on port ${PORT}`);
});
//unhandled rejection:
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Export io instance
module.exports = { app, io };