diff --git a/src/chatSetup.ts b/src/chatSetup.ts index 0ee42a05..df473bc9 100644 --- a/src/chatSetup.ts +++ b/src/chatSetup.ts @@ -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); @@ -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 }); @@ -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) => { diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index 7177d420..334ac109 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -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); diff --git a/src/server.ts b/src/server.ts index 0121b0fe..3b0ade65 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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'; @@ -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);