Skip to content

Commit

Permalink
chore(refactor): reduce parsing query params unnecessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
kangmingtay committed Dec 12, 2024
1 parent 039727d commit b0e7050
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,8 @@ export default class GoTrueClient {
*/
private async _initialize(): Promise<InitializeResult> {
try {
const isPKCEFlow = isBrowser() ? await this._isPKCEFlow() : false
this._debug('#_initialize()', 'begin', 'is PKCE flow', isPKCEFlow)

if (isPKCEFlow || (this.detectSessionInUrl && this._isImplicitGrantFlow())) {
const { data, error } = await this._getSessionFromURL(isPKCEFlow)
if (isBrowser() && this.detectSessionInUrl) {
const { data, error } = await this._getSessionFromURL()
if (error) {
this._debug('#_initialize()', 'error detecting session from URL', error)

Expand Down Expand Up @@ -1414,7 +1411,7 @@ export default class GoTrueClient {
/**
* Gets the session data from a URL string
*/
private async _getSessionFromURL(isPKCEFlow: boolean): Promise<
private async _getSessionFromURL(): Promise<
| {
data: { session: Session; redirectType: string | null }
error: null
Expand All @@ -1439,14 +1436,23 @@ export default class GoTrueClient {
)
}

const isRedirectFromImplicitGrantFlow = this._isImplicitGrantFlow(params)
const isRedirectFromPKCEFlow = await this._isPKCEFlow(params)

// Checks for mismatches between the flowType initialised in the client and the URL parameters
if (this.flowType === 'implicit' && !this._isImplicitGrantFlow()) {
throw new AuthImplicitGrantRedirectError('Not a valid implicit grant flow url.')
} else if (this.flowType == 'pkce' && !isPKCEFlow) {
throw new AuthPKCEGrantCodeExchangeError('Not a valid PKCE flow url.')
if (!isRedirectFromImplicitGrantFlow && !isRedirectFromPKCEFlow) {
if (this.flowType === 'implicit') {
throw new AuthImplicitGrantRedirectError('Not a valid implicit grant flow url.')
} else if (this.flowType === 'pkce') {
throw new AuthPKCEGrantCodeExchangeError('Not a valid PKCE flow url.')
} else {
throw new AuthError('Invalid flow type.')
}
}

if (isPKCEFlow) {
// Since this is a redirect for PKCE, we attempt to retrieve the code from the URL for the code exchange
if (isRedirectFromPKCEFlow) {
this._debug('#_initialize()', 'begin', 'is PKCE flow', isRedirectFromPKCEFlow)
if (!params.code) throw new AuthPKCEGrantCodeExchangeError('No code detected.')
const { data, error } = await this._exchangeCodeForSession(params.code)
if (error) throw error
Expand Down Expand Up @@ -1536,24 +1542,20 @@ export default class GoTrueClient {
/**
* Checks if the current URL contains parameters given by an implicit oauth grant flow (https://www.rfc-editor.org/rfc/rfc6749.html#section-4.2)
*/
private _isImplicitGrantFlow(): boolean {
const params = parseParametersFromURL(window.location.href)

return !!(isBrowser() && (params.access_token || params.error_description))
private _isImplicitGrantFlow(params: { [parameter: string]: string }): boolean {
return !!((params.access_token || params.error_description) && this.flowType === 'implicit')
}

/**
* Checks if the current URL and backing storage contain parameters given by a PKCE flow
*/
private async _isPKCEFlow(): Promise<boolean> {
const params = parseParametersFromURL(window.location.href)

private async _isPKCEFlow(params: { [parameter: string]: string }): Promise<boolean> {
const currentStorageContent = await getItemAsync(
this.storage,
`${this.storageKey}-code-verifier`
)

return !!(params.code && currentStorageContent)
return !!(params.code && currentStorageContent && this.flowType === 'pkce')
}

/**
Expand Down

0 comments on commit b0e7050

Please sign in to comment.