File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments