Skip to content

Commit 089c687

Browse files
authored
chore(refactor): reduce parsing query params unnecessarily (#993)
## What kind of change does this PR introduce? * Refactor `_getSessionFromURL`, `_isPKCEFlow` and `_isImplicitGrantFlow` - previously we were parsing the url parameters multiple times which was unnecessary. Now, we parse it once and pass it in as an argument in other functions that need it. * Makes the code more readable since `getSessionFromURL` should only be called in a browser context and if `detectSessionInURL` is set.
1 parent 9f32d30 commit 089c687

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/GoTrueClient.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,8 @@ export default class GoTrueClient {
307307
*/
308308
private async _initialize(): Promise<InitializeResult> {
309309
try {
310-
const isPKCEFlow = isBrowser() ? await this._isPKCEFlow() : false
311-
this._debug('#_initialize()', 'begin', 'is PKCE flow', isPKCEFlow)
312-
313-
if (isPKCEFlow || (this.detectSessionInUrl && this._isImplicitGrantFlow())) {
314-
const { data, error } = await this._getSessionFromURL(isPKCEFlow)
310+
if (isBrowser() && this.detectSessionInUrl) {
311+
const { data, error } = await this._getSessionFromURL()
315312
if (error) {
316313
this._debug('#_initialize()', 'error detecting session from URL', error)
317314

@@ -1414,7 +1411,7 @@ export default class GoTrueClient {
14141411
/**
14151412
* Gets the session data from a URL string
14161413
*/
1417-
private async _getSessionFromURL(isPKCEFlow: boolean): Promise<
1414+
private async _getSessionFromURL(): Promise<
14181415
| {
14191416
data: { session: Session; redirectType: string | null }
14201417
error: null
@@ -1439,14 +1436,23 @@ export default class GoTrueClient {
14391436
)
14401437
}
14411438

1439+
const isRedirectFromImplicitGrantFlow = this._isImplicitGrantFlow(params)
1440+
const isRedirectFromPKCEFlow = await this._isPKCEFlow(params)
1441+
14421442
// Checks for mismatches between the flowType initialised in the client and the URL parameters
1443-
if (this.flowType === 'implicit' && !this._isImplicitGrantFlow()) {
1444-
throw new AuthImplicitGrantRedirectError('Not a valid implicit grant flow url.')
1445-
} else if (this.flowType == 'pkce' && !isPKCEFlow) {
1446-
throw new AuthPKCEGrantCodeExchangeError('Not a valid PKCE flow url.')
1443+
if (!isRedirectFromImplicitGrantFlow && !isRedirectFromPKCEFlow) {
1444+
if (this.flowType === 'implicit') {
1445+
throw new AuthImplicitGrantRedirectError('Not a valid implicit grant flow url.')
1446+
} else if (this.flowType === 'pkce') {
1447+
throw new AuthPKCEGrantCodeExchangeError('Not a valid PKCE flow url.')
1448+
} else {
1449+
throw new AuthError('Invalid flow type.')
1450+
}
14471451
}
14481452

1449-
if (isPKCEFlow) {
1453+
// Since this is a redirect for PKCE, we attempt to retrieve the code from the URL for the code exchange
1454+
if (isRedirectFromPKCEFlow) {
1455+
this._debug('#_initialize()', 'begin', 'is PKCE flow', isRedirectFromPKCEFlow)
14501456
if (!params.code) throw new AuthPKCEGrantCodeExchangeError('No code detected.')
14511457
const { data, error } = await this._exchangeCodeForSession(params.code)
14521458
if (error) throw error
@@ -1536,24 +1542,20 @@ export default class GoTrueClient {
15361542
/**
15371543
* 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)
15381544
*/
1539-
private _isImplicitGrantFlow(): boolean {
1540-
const params = parseParametersFromURL(window.location.href)
1541-
1542-
return !!(isBrowser() && (params.access_token || params.error_description))
1545+
private _isImplicitGrantFlow(params: { [parameter: string]: string }): boolean {
1546+
return !!((params.access_token || params.error_description) && this.flowType === 'implicit')
15431547
}
15441548

15451549
/**
15461550
* Checks if the current URL and backing storage contain parameters given by a PKCE flow
15471551
*/
1548-
private async _isPKCEFlow(): Promise<boolean> {
1549-
const params = parseParametersFromURL(window.location.href)
1550-
1552+
private async _isPKCEFlow(params: { [parameter: string]: string }): Promise<boolean> {
15511553
const currentStorageContent = await getItemAsync(
15521554
this.storage,
15531555
`${this.storageKey}-code-verifier`
15541556
)
15551557

1556-
return !!(params.code && currentStorageContent)
1558+
return !!(params.code && currentStorageContent && this.flowType === 'pkce')
15571559
}
15581560

15591561
/**

0 commit comments

Comments
 (0)