feat(auth): add optional accessToken parameter to setSession()#1327
Open
feat(auth): add optional accessToken parameter to setSession()#1327
Conversation
Mirrors the JS SDK behaviour: when both tokens are supplied and the access token has not yet expired, the session is restored directly (via a getUser() call) without an extra /token refresh round-trip. If the access token is expired or omitted the existing _callRefreshToken path is preserved unchanged. - Valid, non-expired accessToken: calls getUser(accessToken), builds Session locally, fires AuthChangeEvent.signedIn. - Expired accessToken: falls back to _callRefreshToken(refreshToken). - Malformed accessToken: throws AuthInvalidJwtException. - Empty accessToken: throws AuthSessionMissingException. Linear: SDK-784 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR extends the GoTrue Dart client’s setSession() API to optionally accept an accessToken alongside a refreshToken, allowing session restoration via /user when the access token is still valid (mirroring supabase-js behavior) and falling back to the existing refresh-token flow otherwise.
Changes:
- Added
accessTokenoptional named parameter toGoTrueClient.setSession()with JWT expiry check +/userfetch path. - Persisted session + emitted
SIGNED_INwhen restoring directly from provided tokens. - Added tests for dual-token success path, expired-token fallback, and invalid/empty access token errors.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/gotrue/lib/src/gotrue_client.dart | Adds the optional accessToken path to setSession() with JWT exp check, getUser call, and session persistence/event emission. |
| packages/gotrue/test/client_test.dart | Adds integration tests covering the new setSession(refreshToken, accessToken: ...) behaviors and error cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Mar 25, 2026
* Initial plan * docs: clarify setSession skips /token refresh but still calls /user Co-authored-by: grdsdev <5923044+grdsdev@users.noreply.github.com> Agent-Logs-Url: https://github.com/supabase/supabase-flutter/sessions/db643f38-73ec-4b87-92e3-9d995d319a73 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: grdsdev <5923044+grdsdev@users.noreply.github.com>
…n() (#1331) * Initial plan * fix(auth): apply expiryMargin buffer to hasExpired check in setSession() Co-authored-by: grdsdev <5923044+grdsdev@users.noreply.github.com> Agent-Logs-Url: https://github.com/supabase/supabase-flutter/sessions/80e8e775-47d3-4e08-aa75-07f715c27411 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: grdsdev <5923044+grdsdev@users.noreply.github.com>
- Use exp - iat for expiresIn (full token lifetime, not remaining seconds) - Deduplicate concurrent fast-path calls via _refreshTokenCompleter - Use idiomatic throwsA in error-throwing tests Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- New set_session_test.dart with 10 mock-based tests covering: - Validation order (empty RT checked before AT) - Expiry margin boundary (exp within 30 s treated as expired) - JWT without exp claim falls back to refresh - expiresIn = exp - iat when both claims present - expiresIn = null when iat absent - expiresAt matches JWT exp - Preserved access/refresh token values - Fast path fires signedIn, fallback fires tokenRefreshed - Concurrent calls deduplicated to one /user request - signedIn fires exactly once under concurrent load - Fix remaining try/catch in client_test.dart to use throwsA Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
dart_jsonwebtoken auto-injects an iat claim when signing, which broke: - 'expiresIn equals exp minus iat' (iat value was overridden) - 'expiresIn is null when iat absent' (iat was auto-added) Replace dart_jsonwebtoken usage with _makeRawJwt(), which base64url-encodes the payload map directly (no auto-claims, no signature verification needed). Also removes the dart_jsonwebtoken import from the test file. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional `accessToken` named parameter to `setSession()` in `GoTrueClient`, mirroring the JS SDK behaviour.
When both tokens are provided and the access token is still valid, the session is restored by calling `GET /user` with the supplied access token — skipping the `POST /token?grant_type=refresh_token` round-trip. If the access token is expired or omitted, the existing `_callRefreshToken` path is unchanged.
Changes
Behaviour matrix
Test plan
Closes: SDK-784
Fixes: #1298
🤖 Generated with Claude Code
/take