@@ -66,30 +66,31 @@ export async function startWsServer({ jwtSecret, port }) {
66
66
const docName = request . url . slice ( 1 ) . split ( "?" ) [ 0 ] ;
67
67
const token = url . searchParams . get ( "token" ) ;
68
68
const role = url . searchParams . get ( "role" ) ;
69
+ let userId = "" ;
69
70
if ( token ) {
70
71
const decoded = jwt . verify ( token , jwtSecret ) as TokenInterface ;
71
- const userId = decoded . id ;
72
- const permission = await checkPermission ( { docName, userId } ) ;
73
- switch ( permission ) {
74
- case "read" :
75
- // TODO I should disable editing in the frontend as well.
76
- wss . handleUpgrade ( request , socket , head , function done ( ws ) {
77
- wss . emit ( "connection" , ws , request , { readOnly : true , role } ) ;
78
- } ) ;
79
- break ;
80
- case "write" :
81
- wss . handleUpgrade ( request , socket , head , function done ( ws ) {
82
- wss . emit ( "connection" , ws , request , { readOnly : false , role } ) ;
83
- } ) ;
84
- break ;
85
- case "none" :
86
- // This should not happen. This should be blocked by frontend code.
87
- socket . write ( "HTTP/1.1 401 Unauthorized\r\n\r\n" ) ;
88
- socket . destroy ( ) ;
89
- return ;
90
- }
91
- return ;
72
+ userId = decoded . id ;
92
73
}
74
+ const permission = await checkPermission ( { docName, userId } ) ;
75
+ switch ( permission ) {
76
+ case "read" :
77
+ // TODO I should disable editing in the frontend as well.
78
+ wss . handleUpgrade ( request , socket , head , function done ( ws ) {
79
+ wss . emit ( "connection" , ws , request , { readOnly : true , role } ) ;
80
+ } ) ;
81
+ break ;
82
+ case "write" :
83
+ wss . handleUpgrade ( request , socket , head , function done ( ws ) {
84
+ wss . emit ( "connection" , ws , request , { readOnly : false , role } ) ;
85
+ } ) ;
86
+ break ;
87
+ case "none" :
88
+ // This should not happen. This should be blocked by frontend code.
89
+ socket . write ( "HTTP/1.1 401 Unauthorized\r\n\r\n" ) ;
90
+ socket . destroy ( ) ;
91
+ return ;
92
+ }
93
+ return ;
93
94
}
94
95
socket . write ( "HTTP/1.1 401 Unauthorized\r\n\r\n" ) ;
95
96
socket . destroy ( ) ;
0 commit comments