Skip to content

Commit

Permalink
fix: call realtime and auth setAuth on initial SIGNED_IN event
Browse files Browse the repository at this point in the history
  • Loading branch information
w3b6x9 committed Nov 24, 2021
1 parent c11e7de commit 5034d6f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/SupabaseClient.ts
Expand Up @@ -36,6 +36,7 @@ export default class SupabaseClient {
protected realtime: RealtimeClient
protected multiTab: boolean
protected fetch?: Fetch
protected changedAccessToken: string | undefined

/**
* Create a new client for use in the browser.
Expand Down Expand Up @@ -266,19 +267,21 @@ export default class SupabaseClient {
token: string | undefined,
source: 'CLIENT' | 'STORAGE'
) {
if (token == this.auth.session()?.access_token) {
// Token is unchanged
return null
} else if (event === 'SIGNED_OUT' || event === 'USER_DELETED') {
// Token is removed
this.removeAllSubscriptions()
if (source == 'STORAGE') this.auth.signOut()
} else if (event === 'TOKEN_REFRESHED' || event === 'SIGNED_IN') {
if (
(event === 'TOKEN_REFRESHED' || event === 'SIGNED_IN') &&
this.changedAccessToken !== token
) {
// Token has changed
this.realtime.setAuth(token!)
// Ideally we should call this.auth.recoverSession() - need to make public
// to trigger a "SIGNED_IN" event on this client.
if (source == 'STORAGE') this.auth.setAuth(token!)

this.changedAccessToken = token
} else if (event === 'SIGNED_OUT' || event === 'USER_DELETED') {
// Token is removed
this.removeAllSubscriptions()
if (source == 'STORAGE') this.auth.signOut()
}
}
}

0 comments on commit 5034d6f

Please sign in to comment.