Skip to content

Commit 5e1e477

Browse files
fix: call set auth for realtime on token changes (#1329)
call set auth for realtime on token changes --------- Co-authored-by: Andrew Valleteau <[email protected]>
1 parent 325c2c9 commit 5e1e477

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/SupabaseClient.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,26 @@ export default class SupabaseClient<
318318
}
319319

320320
private _listenForAuthEvents() {
321-
let data = this.auth.onAuthStateChange((event, session) => {
322-
this._handleTokenChanged(event, 'CLIENT', session?.access_token)
321+
let data = this.auth.onAuthStateChange(async (event, session) => {
322+
await this._handleTokenChanged(event, 'CLIENT', session?.access_token)
323323
})
324324
return data
325325
}
326326

327-
private _handleTokenChanged(
327+
private async _handleTokenChanged(
328328
event: AuthChangeEvent,
329329
source: 'CLIENT' | 'STORAGE',
330330
token?: string
331331
) {
332+
// On token change, call Realtime's `setAuth` to sync auth and connections.
333+
// Realtime handles token retrieval internally which may involve customizations, so we don't pass the token directly.
334+
await this.realtime.setAuth()
332335
if (
333336
(event === 'TOKEN_REFRESHED' || event === 'SIGNED_IN') &&
334337
this.changedAccessToken !== token
335338
) {
336339
this.changedAccessToken = token
337340
} else if (event === 'SIGNED_OUT') {
338-
this.realtime.setAuth()
339341
if (source == 'STORAGE') this.auth.signOut()
340342
this.changedAccessToken = undefined
341343
}

0 commit comments

Comments
 (0)