diff --git a/src/GoTrueClient.ts b/src/GoTrueClient.ts index 6337df87f..bd45d36f7 100644 --- a/src/GoTrueClient.ts +++ b/src/GoTrueClient.ts @@ -499,8 +499,16 @@ export default class GoTrueClient { }) } - private async _exchangeCodeForSession(authCode: string): Promise { - const codeVerifier = await getItemAsync(this.storage, `${this.storageKey}-code-verifier`) + private async _exchangeCodeForSession(authCode: string): Promise< + | { + data: { session: Session; user: User; redirectType: string | null } + error: null + } + | { data: { session: null; user: null; redirectType: null }; error: AuthError } + > { + const [codeVerifier, redirectType] = ( + (await getItemAsync(this.storage, `${this.storageKey}-code-verifier`)) as string + ).split('/') const { data, error } = await _request( this.fetch, 'POST', @@ -516,15 +524,18 @@ export default class GoTrueClient { ) await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`) if (error) { - return { data: { user: null, session: null }, error } + return { data: { user: null, session: null, redirectType: null }, error } } else if (!data || !data.session || !data.user) { - return { data: { user: null, session: null }, error: new AuthInvalidTokenResponseError() } + return { + data: { user: null, session: null, redirectType: null }, + error: new AuthInvalidTokenResponseError(), + } } if (data.session) { await this._saveSession(data.session) await this._notifyAllSubscribers('SIGNED_IN', data.session) } - return { data, error } + return { data: { ...data, redirectType: redirectType ?? null }, error } } /** @@ -1538,7 +1549,11 @@ export default class GoTrueClient { let codeChallengeMethod: string | null = null if (this.flowType === 'pkce') { const codeVerifier = generatePKCEVerifier() - await setItemAsync(this.storage, `${this.storageKey}-code-verifier`, codeVerifier) + await setItemAsync( + this.storage, + `${this.storageKey}-code-verifier`, + `${codeVerifier}/PASSWORD_RECOVERY` + ) codeChallenge = await generatePKCEChallenge(codeVerifier) codeChallengeMethod = codeVerifier === codeChallenge ? 'plain' : 's256' }