@@ -307,11 +307,8 @@ export default class GoTrueClient {
307
307
*/
308
308
private async _initialize ( ) : Promise < InitializeResult > {
309
309
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 ( )
315
312
if ( error ) {
316
313
this . _debug ( '#_initialize()' , 'error detecting session from URL' , error )
317
314
@@ -1414,7 +1411,7 @@ export default class GoTrueClient {
1414
1411
/**
1415
1412
* Gets the session data from a URL string
1416
1413
*/
1417
- private async _getSessionFromURL ( isPKCEFlow : boolean ) : Promise <
1414
+ private async _getSessionFromURL ( ) : Promise <
1418
1415
| {
1419
1416
data : { session : Session ; redirectType : string | null }
1420
1417
error : null
@@ -1439,14 +1436,23 @@ export default class GoTrueClient {
1439
1436
)
1440
1437
}
1441
1438
1439
+ const isRedirectFromImplicitGrantFlow = this . _isImplicitGrantFlow ( params )
1440
+ const isRedirectFromPKCEFlow = await this . _isPKCEFlow ( params )
1441
+
1442
1442
// 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
+ }
1447
1451
}
1448
1452
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 )
1450
1456
if ( ! params . code ) throw new AuthPKCEGrantCodeExchangeError ( 'No code detected.' )
1451
1457
const { data, error } = await this . _exchangeCodeForSession ( params . code )
1452
1458
if ( error ) throw error
@@ -1536,24 +1542,20 @@ export default class GoTrueClient {
1536
1542
/**
1537
1543
* 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)
1538
1544
*/
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' )
1543
1547
}
1544
1548
1545
1549
/**
1546
1550
* Checks if the current URL and backing storage contain parameters given by a PKCE flow
1547
1551
*/
1548
- private async _isPKCEFlow ( ) : Promise < boolean > {
1549
- const params = parseParametersFromURL ( window . location . href )
1550
-
1552
+ private async _isPKCEFlow ( params : { [ parameter : string ] : string } ) : Promise < boolean > {
1551
1553
const currentStorageContent = await getItemAsync (
1552
1554
this . storage ,
1553
1555
`${ this . storageKey } -code-verifier`
1554
1556
)
1555
1557
1556
- return ! ! ( params . code && currentStorageContent )
1558
+ return ! ! ( params . code && currentStorageContent && this . flowType === 'pkce' )
1557
1559
}
1558
1560
1559
1561
/**
0 commit comments