Skip to content

Commit 6f0548e

Browse files
committed
fix: deploy issues socketio
1 parent a11b5ea commit 6f0548e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

lib/socket-auth.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Server } from "socket.io";
2+
3+
export const initializeSocketAuth = (io: Server, log: any) => {
4+
io.use(async (socket, next) => {
5+
try {
6+
log.info("Authenticating socket connection");
7+
const { userId, email, role } = socket.handshake.auth;
8+
log.info("socket.handshake.auth:", socket.handshake.auth);
9+
10+
if (!userId || !email || !role) {
11+
throw new Error("Missing authentication data");
12+
}
13+
14+
socket.data.user = { id: userId, email, role };
15+
log.info("socket user object set");
16+
next();
17+
} catch (error) {
18+
log.error("Socket authentication failed:", error);
19+
next(new Error("Authentication failed"));
20+
}
21+
});
22+
};

lib/socket-events.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as userService from "../services/user-service";
2+
3+
export const setupSocketEvents = (socket: any, log: any) => {
4+
socket.on("getDashboardData", async () => {
5+
log.info("Received getDashboardData request from client");
6+
try {
7+
const dashboardData = await userService.getUserStats(socket.data.user.id);
8+
log.info("Sending dashboard data event to client");
9+
socket.emit("dashboardData", dashboardData);
10+
} catch (error) {
11+
log.error("Error fetching dashboard data:", error);
12+
socket.emit("dashboardError", "Failed to fetch dashboard data");
13+
}
14+
});
15+
};

0 commit comments

Comments
 (0)