You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add onTokenSync hook for auth token re-sync (#1001)
* init token sync
* refactor: use existing auth message type for token sync
* refactor(provider): sentToken onOpen and improve testing
* docs(onTokenSync): init onTokenSync in docs
* refactor: close WSconn if onTokenSync throws an err
* refactor: close connection not the ws
Copy file name to clipboardExpand all lines: docs/server/hooks.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,7 @@ By way of illustration, if a user isn’t allowed to connect: Just throw an erro
34
34
|`onConnect`| When a connection is established |[Read more](/server/hooks#on-connect)|
35
35
|`connected`| After a connection has been establied |[Read more](/server/hooks#connected)|
36
36
|`onAuthenticate`| When authentication is required |[Read more](/server/hooks#on-authenticate)|
37
+
|`onTokenSync`| When token synchronization occurs |[Read more](/server/hooks#on-token-sync)|
37
38
|`onAwarenessUpdate`| When awareness changed |[Read more](/server/hooks#on-awareness-update)|
38
39
|`onLoadDocument`| During the creation of a new document |[Read more](/server/hooks#on-load-document)|
39
40
|`afterLoadDocument`| After a document is created |[Read more](/server/hooks#after-load-document)|
@@ -213,6 +214,73 @@ const server = new Server({
213
214
server.listen();
214
215
```
215
216
217
+
### onTokenSync
218
+
219
+
The `onTokenSync` hook is called when the server receives a token synchronization request from a connected provider. This enables the server to validate user tokens during active sessions without requiring a full reconnection.
220
+
221
+
**Hook payload**
222
+
223
+
The `data` passed to the `onTokenSync` hook has the following attributes:
@@ -103,11 +104,19 @@ export class MessageReceiver {
103
104
break;
104
105
}
105
106
106
-
caseMessageType.Auth:
107
+
caseMessageType.Auth: {
108
+
constauthType=message.readVarUint();
109
+
if(authType===AuthMessageType.Token){
110
+
connection?.callbacks.onTokenSyncCallback({
111
+
token: message.readVarString(),
112
+
});
113
+
break;
114
+
}
107
115
console.error(
108
116
"Received an authentication message on a connection that is already fully authenticated. Probably your provider has been destroyed + recreated really fast.",
0 commit comments