Skip to content

fix(auth): read implicit grant params from the URL fragment - #1665

Closed
hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/auth-implicit-grant-url-fragment
Closed

hsusul wants to merge 1 commit into
supabase:mainfrom
hsusul:fix/auth-implicit-grant-url-fragment

Conversation

@hsusul

@hsusul hsusul commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

Closes #1664

What

initialize_from_url() / initialize(url=...) only parsed the URL query string, but GoTrue's implicit grant redirect puts access_token, refresh_token, and error params in the fragment (#access_token=...). Real callback URLs were never recognised, so no session was saved and no SIGNED_IN event fired.

Fix

A new parse_url_params() helper in helpers.py merges fragment and query params, with the query taking precedence (the same as supabase-js parseParametersFromURL). _is_implicit_grant_flow and _get_session_from_url use it in both the async and sync clients. Query-only URLs behave as before.

Tests

  • Added test_initialize_from_url_reads_tokens_from_fragment (async + sync). It fails on main (get_user called 0 times) and passes with the fix.
  • pytest in src/auth against the docker compose infra: 166 passed.
  • mypy src/supabase_auth tests, ruff check, ruff format --check, git diff --check: clean.

GoTrue's implicit grant redirect puts access_token, refresh_token and
error parameters in the URL fragment, but initialize_from_url() only
parsed the query string, so real callback URLs were never recognised.

Parse both the fragment and the query, with the query taking
precedence, matching supabase-js.

Closes supabase#1664
@hsusul
hsusul requested review from a team and o-santi as code owners September 23, 2026 22:49
@o-santi o-santi closed this Oct 19, 2026
spydon added a commit that referenced this pull request Nov 3, 2026
Fixes SDK-2670. Reopens the fix for SDK-1954 / #1664.

## Problem

`initialize(url=...)` and `initialize_from_url()` only read
`urlparse(url).query`. The implicit grant redirect from Supabase Auth
puts its parameters in the fragment
(`#access_token=...&refresh_token=...`, or
`#error=...&error_description=...` on failure). If you passed in the
real callback URL, nothing happened: you got no session, no `SIGNED_IN`
event and no error.

## Fix

A new private `_parse_url_params()` helper in `helpers.py` merges the
fragment and query parameters, and the query wins on conflicts. This
matches `parseParametersFromURL` in auth-js. `_is_implicit_grant_flow()`
and `_get_session_from_url()` both use it now. A PKCE callback
(`?code=...`) has no `access_token` or `error_description`, so it is
still not treated as an implicit grant.

## Relation to #1665

#1665 made the same change and was closed without being merged. It had
no review, no comments and no stated reason. Because of that, #1664 and
SDK-1954 were marked done while the bug is still on `main`. This PR
applies the same approach and adds wider test coverage.

## Tests

New tests in `tests/_async/test_initialize_from_url.py` (and the
generated sync version) mock HTTP with `httpx.MockTransport`, so they
need no infra:

- Tokens in the fragment produce a session and `SIGNED_IN`.
- `type=recovery` in the fragment also fires `PASSWORD_RECOVERY`.
- A fragment error redirect raises `AuthImplicitGrantRedirectError` with
its details.
- Query parameters win over fragment parameters.
- Query-only URLs keep working.
- A PKCE `?code=` callback is not treated as an implicit grant.

The four fragment tests fail on `main` and pass with this change. mypy
is clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

initialize_from_url ignores implicit grant tokens in the URL fragment

2 participants