Skip to content

Commit 9f32d30

Browse files
authored
fix: return error early for redirects (#992)
## What kind of change does this PR introduce? * When using the PKCE flow, if there's a redirect error after the callback and a code is not returned, `getSessionFromUrl` ends up returning `Not a valid PKCE flow url.` instead of the actual error
1 parent 2e6e07c commit 9f32d30

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/GoTrueClient.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,14 +1423,29 @@ export default class GoTrueClient {
14231423
> {
14241424
try {
14251425
if (!isBrowser()) throw new AuthImplicitGrantRedirectError('No browser detected.')
1426+
1427+
const params = parseParametersFromURL(window.location.href)
1428+
1429+
// If there's an error in the URL, it doesn't matter what flow it is, we just return the error.
1430+
if (params.error || params.error_description || params.error_code) {
1431+
// The error class returned implies that the redirect is from an implicit grant flow
1432+
// but it could also be from a redirect error from a PKCE flow.
1433+
throw new AuthImplicitGrantRedirectError(
1434+
params.error_description || 'Error in URL with unspecified error_description',
1435+
{
1436+
error: params.error || 'unspecified_error',
1437+
code: params.error_code || 'unspecified_code',
1438+
}
1439+
)
1440+
}
1441+
1442+
// Checks for mismatches between the flowType initialised in the client and the URL parameters
14261443
if (this.flowType === 'implicit' && !this._isImplicitGrantFlow()) {
14271444
throw new AuthImplicitGrantRedirectError('Not a valid implicit grant flow url.')
14281445
} else if (this.flowType == 'pkce' && !isPKCEFlow) {
14291446
throw new AuthPKCEGrantCodeExchangeError('Not a valid PKCE flow url.')
14301447
}
14311448

1432-
const params = parseParametersFromURL(window.location.href)
1433-
14341449
if (isPKCEFlow) {
14351450
if (!params.code) throw new AuthPKCEGrantCodeExchangeError('No code detected.')
14361451
const { data, error } = await this._exchangeCodeForSession(params.code)
@@ -1444,16 +1459,6 @@ export default class GoTrueClient {
14441459
return { data: { session: data.session, redirectType: null }, error: null }
14451460
}
14461461

1447-
if (params.error || params.error_description || params.error_code) {
1448-
throw new AuthImplicitGrantRedirectError(
1449-
params.error_description || 'Error in URL with unspecified error_description',
1450-
{
1451-
error: params.error || 'unspecified_error',
1452-
code: params.error_code || 'unspecified_code',
1453-
}
1454-
)
1455-
}
1456-
14571462
const {
14581463
provider_token,
14591464
provider_refresh_token,

0 commit comments

Comments
 (0)