Skip to content

Commit

Permalink
fix: update pkce error
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Nov 22, 2024
1 parent dfb40d2 commit 7267f5d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,20 @@ export default class GoTrueClient {
}
}

// TODO: come back and type this
private async handlePKCEFlow(params: any) {
if (!params.code) throw new AuthPKCEGrantCodeExchangeError('No code detected.')
const { data, error } = await this._exchangeCodeForSession(params.code)
if (error) throw error

const url = new URL(window.location.href)
url.searchParams.delete('code')

window.history.replaceState(window.history.state, '', url.toString())

return { data: { session: data.session, redirectType: null }, error: null }
}

/**
* Gets the session data from a URL string
*/
Expand All @@ -1424,19 +1438,8 @@ export default class GoTrueClient {
const params = parseParametersFromURL(window.location.href)

if (isPKCEFlow) {
if (!params.code) throw new AuthPKCEGrantCodeExchangeError('No code detected.')
const { data, error } = await this._exchangeCodeForSession(params.code)
if (error) throw error

const url = new URL(window.location.href)
url.searchParams.delete('code')

window.history.replaceState(window.history.state, '', url.toString())

return { data: { session: data.session, redirectType: null }, error: null }
}

if (params.error || params.error_description || params.error_code) {
return await this.handlePKCEFlow(params)
} else if (params.error || params.error_description || params.error_code) {
throw new AuthImplicitGrantRedirectError(
params.error_description || 'Error in URL with unspecified error_description',
{
Expand Down

0 comments on commit 7267f5d

Please sign in to comment.