Skip to content

Commit

Permalink
feat: push access token to every channel during heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
w3b6x9 committed Nov 19, 2021
1 parent 19f49f9 commit db30bf6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/RealtimeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Message = {
const noop = () => {}

export default class RealtimeClient {
accessToken: string | null = null
channels: RealtimeSubscription[] = []
endPoint: string = ''
headers?: { [key: string]: string } = DEFAULT_HEADERS
Expand Down Expand Up @@ -317,6 +318,15 @@ export default class RealtimeClient {
return this.ref.toString()
}

/**
* Sets the JWT access token used for channel subscription authorization and Realtime RLS.
*
* @param token A JWT string.
*/
setAccessToken(token: string) {

This comment has been minimized.

Copy link
@kiwicopple

kiwicopple Nov 19, 2021

Member

in GoTrue this one is called setAuth():
https://github.com/supabase/gotrue-js/blob/ec799e5dc5565ecdb905148b5b0b12a497657bda/src/GoTrueClient.ts#L382

Should we make the naming convention the same?

This comment has been minimized.

Copy link
@w3b6x9

w3b6x9 Nov 19, 2021

Author Member

@kiwicopple good call, will make the change.

This comment has been minimized.

Copy link
@kiwicopple

kiwicopple Nov 19, 2021

Member

Also when setting this, we should push the token again

this.accessToken = token
}

private _onConnOpen() {
this.log('transport', `connected to ${this.endPointURL()}`)
this._flushSendBuffer()
Expand Down Expand Up @@ -391,5 +401,10 @@ export default class RealtimeClient {
payload: {},
ref: this.pendingHeartbeatRef,
})
this.channels.forEach((channel) =>
channel.push(CHANNEL_EVENTS.access_token, {
access_token: this.accessToken,
})
)

This comment has been minimized.

Copy link
@kiwicopple

kiwicopple Nov 19, 2021

Member

let's move this block to the setAuth() function, then replace this line with setAuth(this.accessToken)

}
}
1 change: 1 addition & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export enum CHANNEL_EVENTS {
join = 'phx_join',
reply = 'phx_reply',
leave = 'phx_leave',
access_token = 'access_token',
}

export enum TRANSPORTS {
Expand Down

0 comments on commit db30bf6

Please sign in to comment.