Skip to content

Commit

Permalink
finishes bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanIrad committed Jul 22, 2024
1 parent 43f5f62 commit ca924c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/chatSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import { getUserNames } from './helpers/defaultRoleGenerator';
interface CustomSocket extends Socket {
userId?: string;
}
export const findId = (socket: CustomSocket) => {
export const findId = async (socket: CustomSocket) => {
try {
const { token } = socket.handshake.auth;
if (!token) {
socket.disconnect();
return;
}
const decoded = decodedToken(token);
const id = typeof decoded === 'string' ? decoded : decoded ? decoded.id : null;
if (typeof id === 'string') {
socket.emit('sendUserId', id);
await socket.emit('sendUserId', id);
socket.userId = id;
return id;
} else {
throw new Error('Token is not a string');
// throw new Error('Token is not a string');
logger.error('No Token Provided!');
}
} catch (error) {
logger.error('Error find Id', error);
Expand All @@ -39,7 +44,6 @@ const sentMessage = async (socket: CustomSocket, data: Message, io: Server) => {
if (senderId) {
try {
const { content } = data;
console.log('--------------messages are here,', data);
const { firstName } = await getUserNames(socket.userId as string);

const chat = await Chat.create({ senderId, content });
Expand Down Expand Up @@ -74,9 +78,19 @@ export const socketSetUp = (server: HttpServer) => {
});
// eslint-disable-next-line @typescript-eslint/no-misused-promises
io.use(async (socket: CustomSocket, next) => {
const id = findId(socket);
socket.userId = id;
next();
try {
const id = await findId(socket);
if (typeof id === 'undefined' || id.length < 2) {
socket.disconnect();
return;
}
socket.userId = id;
next();
} catch (err) {
const error = err as Error;
logger.error(error.stack);
next(error);
}
});

io.on('connection', async (socket: CustomSocket) => {
Expand Down
1 change: 0 additions & 1 deletion src/controllers/authController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export const verifyOTP = async (req: Request, res: Response) => {
try {
const data = req.user as JwtPayload;
const token = await userToken(data.id);

res.status(200).json({ ok: true, token });
} catch (error) {
logger.error('VerifyOTP Internal Server Error', error);
Expand Down
11 changes: 10 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Application, Request, Response } from 'express';
import { Application, Request, Response, NextFunction } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import express from 'express';
Expand Down Expand Up @@ -64,10 +64,19 @@ schedulePasswordUpdatePrompts();
databaseConnection();
scheduledTasks();

app.use((err: Error, req: Request, res: Response, next: NextFunction) => {
logger.error(err.stack);
res.status(500).send({
message: err.message,
stack: process.env.NODE_ENV === 'production' ? 'Something went wrong!' : err.stack,
});
});

const PORT = process.env.PORT ?? 3000;
const server = app.listen(PORT, () => {
logger.info(`Server is running on port ${PORT}`);
});

socketSetUp(server);

export const io = socketSetUp(server);

0 comments on commit ca924c0

Please sign in to comment.