diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index aec3608ec..55d4f3e4e 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -86,17 +86,27 @@ export default class GoTrueClient { headers: settings.headers, cookieOptions: settings.cookieOptions, }) + + // an array of promises which load the current user + let initPromises: Array> = [] + this._recoverSession() - this._recoverAndRefresh() + const recoverAndRefresh = this._recoverAndRefresh() + initPromises.push(recoverAndRefresh) // Handle the OAuth redirect if (settings.detectSessionInUrl && isBrowser() && !!getParameterByName('access_token')) { - this.getSessionFromUrl({ storeSession: true }).then(({ error }) => { + const getSessionFromUrl = this.getSessionFromUrl({ storeSession: true }).then(({ error }) => { if (error) { console.error('Error getting session from URL.', error) } }) + initPromises.push(getSessionFromUrl) } + + Promise.all(initPromises).then(() => { + this._notifyAllSubscribers('INITIAL') + }) } /** diff --git a/src/lib/types.ts b/src/lib/types.ts index 4cb014d97..c4f0ddfca 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -11,6 +11,7 @@ export type Provider = | 'twitch' export type AuthChangeEvent = + | 'INITIAL' | 'SIGNED_IN' | 'SIGNED_OUT' | 'USER_UPDATED'