File tree Expand file tree Collapse file tree 3 files changed +45
-6
lines changed
packages/core/realtime-js Expand file tree Collapse file tree 3 files changed +45
-6
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ type Message = {
34
34
const noop = ( ) => { }
35
35
36
36
export default class RealtimeClient {
37
+ accessToken : string | null = null
37
38
channels : RealtimeSubscription [ ] = [ ]
38
39
endPoint : string = ''
39
40
headers ?: { [ key : string ] : string } = DEFAULT_HEADERS
@@ -317,6 +318,21 @@ export default class RealtimeClient {
317
318
return this . ref . toString ( )
318
319
}
319
320
321
+ /**
322
+ * Sets the JWT access token used for channel subscription authorization and Realtime RLS.
323
+ *
324
+ * @param token A JWT string.
325
+ */
326
+ setAuth ( token : string | null ) {
327
+ this . accessToken = token
328
+
329
+ this . channels . forEach ( ( channel ) =>
330
+ channel . push ( CHANNEL_EVENTS . access_token , {
331
+ access_token : token ,
332
+ } )
333
+ )
334
+ }
335
+
320
336
private _onConnOpen ( ) {
321
337
this . log ( 'transport' , `connected to ${ this . endPointURL ( ) } ` )
322
338
this . _flushSendBuffer ( )
@@ -385,11 +401,6 @@ export default class RealtimeClient {
385
401
return
386
402
}
387
403
this . pendingHeartbeatRef = this . makeRef ( )
388
- this . push ( {
389
- topic : 'phoenix' ,
390
- event : 'heartbeat' ,
391
- payload : { } ,
392
- ref : this . pendingHeartbeatRef ,
393
- } )
404
+ this . setAuth ( this . accessToken )
394
405
}
395
406
}
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ export enum CHANNEL_EVENTS {
29
29
join = 'phx_join' ,
30
30
reply = 'phx_reply' ,
31
31
leave = 'phx_leave' ,
32
+ access_token = 'access_token' ,
32
33
}
33
34
34
35
export enum TRANSPORTS {
Original file line number Diff line number Diff line change @@ -444,6 +444,33 @@ describe('makeRef', () => {
444
444
} )
445
445
} )
446
446
447
+ describe ( 'setAuth' , ( ) => {
448
+ beforeEach ( ( ) => {
449
+ socket = new RealtimeClient ( 'wss://example.com/socket' )
450
+ } )
451
+
452
+ afterEach ( async ( ) => {
453
+ await socket . disconnect ( )
454
+ } )
455
+
456
+ it ( 'sets access token and pushes it to channels' , ( ) => {
457
+ const channel1 = socket . channel ( 'test-topic1' )
458
+ const channel2 = socket . channel ( 'test-topic2' )
459
+ const stub1 = sinon . stub ( channel1 , 'push' )
460
+ const stub2 = sinon . stub ( channel2 , 'push' )
461
+
462
+ socket . setAuth ( 'token123' )
463
+
464
+ assert . strictEqual ( socket . accessToken , 'token123' )
465
+ assert . ok ( stub1 . calledWith ( 'access_token' , {
466
+ access_token : 'token123' ,
467
+ } ) )
468
+ assert . ok ( stub2 . calledWith ( 'access_token' , {
469
+ access_token : 'token123' ,
470
+ } ) )
471
+ } )
472
+ } )
473
+
447
474
describe ( 'sendHeartbeat' , ( ) => {
448
475
before ( ( ) => {
449
476
window . XMLHttpRequest = sinon . useFakeXMLHttpRequest ( )
You can’t perform that action at this time.
0 commit comments