@@ -11,12 +11,20 @@ const { setPersistence, setupWSConnection } = require('../utils/utility.js');
11
11
12
12
const URL_REGEX = / ^ .* \/ ( [ 0 - 9 a - f ] { 24 } ) \? a c c e s s T o k e n = ( [ a - z A - Z 0 - 9 \- . _ ~ % ] { 1 , } ) $ / ;
13
13
14
- const authorize = async ( ws : WebSocket , request : IncomingMessage ) : Promise < boolean > => {
14
+ /**
15
+ * Verifies the user's access to a specific room by validating the JWT token,
16
+ * checking the room's status, and ensuring the user has not forfeited.
17
+ * Returns `roomId` if the access is authorized, or `null` otherwise.
18
+ * @param ws
19
+ * @param request
20
+ * @returns
21
+ */
22
+ const authorize = async ( ws : WebSocket , request : IncomingMessage ) : Promise < string | null > => {
15
23
const url = request . url ?? '' ;
16
24
const match = url ?. match ( URL_REGEX ) ;
17
25
if ( ! match ) {
18
26
handleAuthFailed ( ws , 'Authorization failed: Invalid format' ) ;
19
- return false ;
27
+ return null ;
20
28
}
21
29
const roomId = match [ 1 ] ;
22
30
const accessToken = match [ 2 ] ;
@@ -34,27 +42,27 @@ const authorize = async (ws: WebSocket, request: IncomingMessage): Promise<boole
34
42
} ) ;
35
43
if ( ! user ) {
36
44
handleAuthFailed ( ws , 'Authorization failed: Invalid token' ) ;
37
- return false ;
45
+ return null ;
38
46
}
39
47
40
48
const room = await findRoomById ( roomId , user . id ) ;
41
49
if ( ! room ) {
42
50
handleAuthFailed ( ws , 'Authorization failed' ) ;
43
- return false ;
51
+ return null ;
44
52
}
45
53
46
54
if ( ! room . room_status ) {
47
55
handleRoomClosed ( ws ) ;
48
- return false ;
56
+ return null ;
49
57
}
50
58
51
59
const userInRoom = room . users . find ( ( u : { id : string } ) => u . id === user . id ) ;
52
60
if ( userInRoom ?. isForfeit ) {
53
61
handleAuthFailed ( ws , 'Authorization failed: User has forfeited' ) ;
54
- return false ;
62
+ return null ;
55
63
}
56
64
console . log ( 'WebSocket connection established for room:' , roomId ) ;
57
- return true ;
65
+ return roomId ;
58
66
} ;
59
67
60
68
/**
@@ -65,13 +73,13 @@ export const startWebSocketServer = (server: Server) => {
65
73
const wss = new WebSocketServer ( { server } ) ;
66
74
67
75
wss . on ( 'connection' , async ( conn : WebSocket , req : IncomingMessage ) => {
68
- const isAuthorized = await authorize ( conn , req ) ;
69
- if ( ! isAuthorized ) {
76
+ const roomId = await authorize ( conn , req ) ;
77
+ if ( ! roomId ) {
70
78
return ;
71
79
}
72
80
73
81
try {
74
- setupWSConnection ( conn , req ) ;
82
+ setupWSConnection ( conn , req , { docName : roomId } ) ;
75
83
} catch ( error ) {
76
84
console . error ( 'Failed to set up WebSocket connection:' , error ) ;
77
85
handleAuthFailed ( conn , 'Authorization failed' ) ;
@@ -85,7 +93,7 @@ export const startWebSocketServer = (server: Server) => {
85
93
console . log ( `Loaded persisted document for ${ docName } ` ) ;
86
94
87
95
const newUpdates = Y . encodeStateAsUpdate ( ydoc ) ;
88
- mdb . storeUpdate ( docName , newUpdates ) ;
96
+ await mdb . storeUpdate ( docName , newUpdates ) ;
89
97
90
98
Y . applyUpdate ( ydoc , Y . encodeStateAsUpdate ( persistedYdoc ) ) ;
91
99
0 commit comments