Skip to content

Commit

Permalink
fix: don't throw error in exchangeCodeForSession (#946)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Fixes #782 
* Supercedes #945 

## What is the current behavior?

Please link any relevant issues here.

## What is the new behavior?

Feel free to include screenshots if it includes visual changes.

## Additional context

Add any other context or screenshots.
  • Loading branch information
kangmingtay authored Aug 27, 2024
1 parent e2337ba commit 6e161ec
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,33 +580,43 @@ export default class GoTrueClient {
> {
const storageItem = await getItemAsync(this.storage, `${this.storageKey}-code-verifier`)
const [codeVerifier, redirectType] = ((storageItem ?? '') as string).split('/')
const { data, error } = await _request(
this.fetch,
'POST',
`${this.url}/token?grant_type=pkce`,
{
headers: this.headers,
body: {
auth_code: authCode,
code_verifier: codeVerifier,
},
xform: _sessionResponse,

try {
const { data, error } = await _request(
this.fetch,
'POST',
`${this.url}/token?grant_type=pkce`,
{
headers: this.headers,
body: {
auth_code: authCode,
code_verifier: codeVerifier,
},
xform: _sessionResponse,
}
)
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
if (error) {
throw error
}
)
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
if (error) {
return { data: { user: null, session: null, redirectType: null }, error }
} else if (!data || !data.session || !data.user) {
return {
data: { user: null, session: null, redirectType: null },
error: new AuthInvalidTokenResponseError(),
if (!data || !data.session || !data.user) {
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: { ...data, redirectType: redirectType ?? null }, error }
} catch (error) {
if (isAuthError(error)) {
return { data: { user: null, session: null, redirectType: null }, error }
}

throw error
}
if (data.session) {
await this._saveSession(data.session)
await this._notifyAllSubscribers('SIGNED_IN', data.session)
}
return { data: { ...data, redirectType: redirectType ?? null }, error }
}

/**
Expand Down

0 comments on commit 6e161ec

Please sign in to comment.