@ceedcv-maya/shared-realtime-react 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/createEcho.ts +26 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ceedcv-maya/shared-realtime-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
@@ -35,4 +35,4 @@
|
|
|
35
35
|
},
|
|
36
36
|
"description": "Reverb/Echo realtime client for the Maya ecosystem: createEcho() factory + useRealtimeNotifications hook.",
|
|
37
37
|
"sideEffects": false
|
|
38
|
-
}
|
|
38
|
+
}
|
package/src/createEcho.ts
CHANGED
|
@@ -22,9 +22,10 @@ export interface ReverbBootstrapConfig {
|
|
|
22
22
|
/**
|
|
23
23
|
* Resolver for the bearer JWT sent to authEndpoint. Called per authorize
|
|
24
24
|
* request so the client always sends a fresh token (handles silent refresh).
|
|
25
|
+
* May be sync or async — the authorizer awaits the result.
|
|
25
26
|
* Return null to deny the channel without making the request.
|
|
26
27
|
*/
|
|
27
|
-
getBearerToken: () => string | null | undefined
|
|
28
|
+
getBearerToken: () => string | null | undefined | Promise<string | null | undefined>;
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
let instance: Echo<'reverb'> | null = null;
|
|
@@ -69,26 +70,32 @@ export function createEcho(config: ReverbBootstrapConfig): Echo<'reverb'> {
|
|
|
69
70
|
},
|
|
70
71
|
authorizer: (channel) => ({
|
|
71
72
|
authorize: (socketId, callback) => {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
Accept: 'application/json',
|
|
81
|
-
'Content-Type': 'application/json',
|
|
82
|
-
Authorization: `Bearer ${token}`,
|
|
83
|
-
},
|
|
84
|
-
body: JSON.stringify({ socket_id: socketId, channel_name: channel.name }),
|
|
85
|
-
})
|
|
86
|
-
.then(async (response) => {
|
|
87
|
-
if (!response.ok) {
|
|
88
|
-
callback(new Error(`broadcasting_auth_${response.status}`), null);
|
|
73
|
+
// Resolve token first (await Promise if the resolver is async — the
|
|
74
|
+
// shared-auth-react getBearerToken IS async because it triggers a
|
|
75
|
+
// silent refresh; passing the Promise as `${token}` would serialize
|
|
76
|
+
// it as "[object Promise]" and produce a malformed JWT server-side).
|
|
77
|
+
Promise.resolve(config.getBearerToken())
|
|
78
|
+
.then((token) => {
|
|
79
|
+
if (!token) {
|
|
80
|
+
callback(new Error('no_bearer_token'), null);
|
|
89
81
|
return;
|
|
90
82
|
}
|
|
91
|
-
|
|
83
|
+
return fetch(config.authEndpoint, {
|
|
84
|
+
method: 'POST',
|
|
85
|
+
headers: {
|
|
86
|
+
Accept: 'application/json',
|
|
87
|
+
'Content-Type': 'application/json',
|
|
88
|
+
Authorization: `Bearer ${token}`,
|
|
89
|
+
},
|
|
90
|
+
body: JSON.stringify({ socket_id: socketId, channel_name: channel.name }),
|
|
91
|
+
})
|
|
92
|
+
.then(async (response) => {
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
callback(new Error(`broadcasting_auth_${response.status}`), null);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
callback(null, await response.json());
|
|
98
|
+
});
|
|
92
99
|
})
|
|
93
100
|
.catch((err: unknown) => {
|
|
94
101
|
callback(err instanceof Error ? err : new Error('broadcasting_auth_failed'), null);
|