Skip to content

Commit 53decab

Browse files
authored
Merge pull request #468 from supabase/fix/gotrue2-realtime
Fix: Realtime setup with GoTrue v2
2 parents 033ef86 + 13fb8a7 commit 53decab

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/SupabaseClient.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ export default class SupabaseClient<
9797
this.headers = { ...DEFAULT_HEADERS, ...options?.headers }
9898
this.shouldThrowOnError = settings.shouldThrowOnError || false
9999

100+
this.auth = this._initSupabaseAuthClient(settings.auth || {}, this.headers, settings.fetch)
100101
this.fetch = fetchWithAuth(supabaseKey, this._getAccessToken.bind(this), settings.fetch)
101102

102-
this.auth = this._initSupabaseAuthClient(settings.auth || {}, this.headers, this.fetch)
103103
this.realtime = this._initRealtimeClient({ headers: this.headers, ...settings.realtime })
104104
this.rest = new PostgrestClient(`${_supabaseUrl}/rest/v1`, {
105105
headers: this.headers,
@@ -180,13 +180,11 @@ export default class SupabaseClient<
180180
* Creates a channel with Broadcast and Presence.
181181
*/
182182
channel(name: string, opts?: { [key: string]: any }): SupabaseRealtimeClient {
183-
const token = this.realtime.accessToken ?? this.supabaseKey
184-
185183
if (!this.realtime.isConnected()) {
186184
this.realtime.connect()
187185
}
188186

189-
return new SupabaseRealtimeClient(this.realtime, name, token, opts)
187+
return new SupabaseRealtimeClient(this.realtime, name, opts)
190188
}
191189

192190
/**

src/lib/SupabaseRealtimeClient.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ import { RealtimeChannel, RealtimeClient, Transformers } from '@supabase/realtim
22
import { SupabaseRealtimePayload } from './types'
33

44
export class SupabaseRealtimeClient {
5+
socket: RealtimeClient
56
channel: RealtimeChannel
67

7-
constructor(socket: RealtimeClient, name: string, token: string, opts?: { [key: string]: any }) {
8-
let chanParams: Record<string, string> = { user_token: token }
9-
10-
if (opts) {
11-
chanParams = { ...chanParams, ...opts }
12-
}
13-
14-
this.channel = socket.channel(`realtime:${name}`, chanParams) as RealtimeChannel
8+
constructor(socket: RealtimeClient, name: string, opts: { [key: string]: any } = {}) {
9+
this.socket = socket
10+
this.channel = socket.channel(`realtime:${name}`, opts) as RealtimeChannel
1511
}
1612

1713
private getPayloadRecords(payload: any) {
@@ -66,13 +62,30 @@ export class SupabaseRealtimeClient {
6662
* Enables the channel.
6763
*/
6864
subscribe(callback: Function = () => {}) {
65+
// if the socket already has a good accessToken
66+
// we can just use it straight away
67+
if (this.socket.accessToken) {
68+
this.channel.updateJoinPayload({
69+
user_token: this.socket.accessToken,
70+
})
71+
}
72+
6973
this.channel.onError((e: Error) => callback('CHANNEL_ERROR', e))
7074
this.channel.onClose(() => callback('CLOSED'))
7175
this.channel
7276
.subscribe()
73-
.receive('ok', () => callback('SUBSCRIBED'))
77+
.receive('ok', () => {
78+
callback('SUBSCRIBED')
79+
80+
// re-set the accessToken again in case it was set while
81+
// the subscription was isJoining
82+
if (this.socket.accessToken) {
83+
this.socket.setAuth(this.socket.accessToken)
84+
}
85+
})
7486
.receive('error', (e: Error) => callback('CHANNEL_ERROR', e))
7587
.receive('timeout', () => callback('RETRYING_AFTER_TIMEOUT'))
88+
7689
return this.channel
7790
}
7891
}

0 commit comments

Comments
 (0)