@@ -67,12 +67,7 @@ export class MatchGateway implements OnGatewayInit {
67
67
return ;
68
68
}
69
69
70
- // Retrieve the userId associated with the current socket
71
- const storedUserId = [ ...this . userSockets . entries ( ) ] . find (
72
- ( [ , socketId ] ) => socketId === client . id ,
73
- ) ?. [ 0 ] ;
74
- if ( ! storedUserId || storedUserId !== payload . userId ) {
75
- client . emit ( EXCEPTION , 'UserId does not match the current socket.' ) ;
70
+ if ( ! this . validateUserId ( client , payload . userId ) ) {
76
71
return ;
77
72
}
78
73
@@ -107,6 +102,10 @@ export class MatchGateway implements OnGatewayInit {
107
102
return ;
108
103
}
109
104
105
+ if ( ! this . validateUserId ( client , payload . userId ) ) {
106
+ return ;
107
+ }
108
+
110
109
try {
111
110
const result = await firstValueFrom (
112
111
this . matchingClient . send ( 'match-cancel' , { userId : payload . userId } ) ,
@@ -239,4 +238,16 @@ export class MatchGateway implements OnGatewayInit {
239
238
client . emit ( EXCEPTION , `Error connecting to /match socket: ${ message } ` ) ;
240
239
client . disconnect ( ) ;
241
240
}
241
+
242
+ // Method to validate the userId associated with the current socket
243
+ private validateUserId ( client : Socket , userId : string ) : boolean {
244
+ const storedUserId = [ ...this . userSockets . entries ( ) ] . find (
245
+ ( [ , socketId ] ) => socketId === client . id ,
246
+ ) ?. [ 0 ] ;
247
+ if ( ! storedUserId || storedUserId !== userId ) {
248
+ client . emit ( EXCEPTION , 'UserId does not match the current socket.' ) ;
249
+ return false ;
250
+ }
251
+ return true ;
252
+ }
242
253
}
0 commit comments