Skip to content

Commit e9e74f8

Browse files
authored
Merge pull request #102 from atlp-rwanda/bg-fx-socket-error
finishes bug fix
2 parents 0fc3085 + ca924c0 commit e9e74f8

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

Diff for: src/chatSetup.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ import { getUserNames } from './helpers/defaultRoleGenerator';
88
interface CustomSocket extends Socket {
99
userId?: string;
1010
}
11-
export const findId = (socket: CustomSocket) => {
11+
export const findId = async (socket: CustomSocket) => {
1212
try {
1313
const { token } = socket.handshake.auth;
14+
if (!token) {
15+
socket.disconnect();
16+
return;
17+
}
1418
const decoded = decodedToken(token);
1519
const id = typeof decoded === 'string' ? decoded : decoded ? decoded.id : null;
1620
if (typeof id === 'string') {
17-
socket.emit('sendUserId', id);
21+
await socket.emit('sendUserId', id);
1822
socket.userId = id;
1923
return id;
2024
} else {
21-
throw new Error('Token is not a string');
25+
// throw new Error('Token is not a string');
26+
logger.error('No Token Provided!');
2227
}
2328
} catch (error) {
2429
logger.error('Error find Id', error);
@@ -39,7 +44,6 @@ const sentMessage = async (socket: CustomSocket, data: Message, io: Server) => {
3944
if (senderId) {
4045
try {
4146
const { content } = data;
42-
console.log('--------------messages are here,', data);
4347
const { firstName } = await getUserNames(socket.userId as string);
4448

4549
const chat = await Chat.create({ senderId, content });
@@ -74,9 +78,19 @@ export const socketSetUp = (server: HttpServer) => {
7478
});
7579
// eslint-disable-next-line @typescript-eslint/no-misused-promises
7680
io.use(async (socket: CustomSocket, next) => {
77-
const id = findId(socket);
78-
socket.userId = id;
79-
next();
81+
try {
82+
const id = await findId(socket);
83+
if (typeof id === 'undefined' || id.length < 2) {
84+
socket.disconnect();
85+
return;
86+
}
87+
socket.userId = id;
88+
next();
89+
} catch (err) {
90+
const error = err as Error;
91+
logger.error(error.stack);
92+
next(error);
93+
}
8094
});
8195

8296
io.on('connection', async (socket: CustomSocket) => {

Diff for: src/controllers/authController.ts

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ export const verifyOTP = async (req: Request, res: Response) => {
115115
try {
116116
const data = req.user as JwtPayload;
117117
const token = await userToken(data.id);
118-
119118
res.status(200).json({ ok: true, token });
120119
} catch (error) {
121120
logger.error('VerifyOTP Internal Server Error', error);

Diff for: src/server.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Application, Request, Response } from 'express';
1+
import { Application, Request, Response, NextFunction } from 'express';
22
import dotenv from 'dotenv';
33
import cors from 'cors';
44
import express from 'express';
@@ -64,10 +64,19 @@ schedulePasswordUpdatePrompts();
6464
databaseConnection();
6565
scheduledTasks();
6666

67+
app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
68+
logger.error(err.stack);
69+
res.status(500).send({
70+
message: err.message,
71+
stack: process.env.NODE_ENV === 'production' ? 'Something went wrong!' : err.stack,
72+
});
73+
});
74+
6775
const PORT = process.env.PORT ?? 3000;
6876
const server = app.listen(PORT, () => {
6977
logger.info(`Server is running on port ${PORT}`);
7078
});
7179

7280
socketSetUp(server);
81+
7382
export const io = socketSetUp(server);

0 commit comments

Comments
 (0)