Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finishes bug fix #102

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading