Skip to content

fix(account): keep the current session through the OAuth2 token flow - #13602

Merged
Meldiron merged 2 commits into
mainfrom
fix-oauth2-token-flow-keeps-current-session
Sep 10, 2026
Merged

Meldiron merged 2 commits into
mainfrom
fix-oauth2-token-flow-keeps-current-session

Conversation

@Meldiron

Copy link
Copy Markdown
Contributor

What does this PR do?

GET /v1/account/sessions/oauth2/:provider/redirect deletes the caller's current session whenever a signed-in user completes an OAuth2 flow. For createOAuth2Session that is the intended "replace" behavior: the handler immediately creates the new session and sets its cookie, so the browser stays signed in.

createOAuth2Token runs through the same handler but creates no session there. The app is expected to exchange the returned userId + secret through createSession afterwards. Deleting the current session in that flow leaves the caller signed out in between: a signed-in user who connects a provider lands on the success URL as a guest, and any auth guard on that page bounces them to sign-in before the token can be exchanged. We hit this in the Console while building one-click SMTP setup with Resend, and had to add a guest-tolerant callback page that restores the session by hand.

This PR gives the token flow parity with the session flow regarding the caller's session:

  • The current session is deleted only when the flow creates a replacement (createOAuth2Session).
  • In the token flow the identity is still attached to the signed-in account, the existing session stays valid, and the later createSession adds a session next to it.
  • createOAuth2Token docs mention the behavior.

Test Plan

New e2e testCreateOAuth2TokenKeepsCurrentSession in AccountCustomClientTest:

  1. Sign in through the mock provider with the session flow and keep the cookie.
  2. Run the token flow with that cookie on every hop. The redirect lands on the success URL with secret + userId for the same user and sets no cookie.
  3. GET /account/sessions/current with the original cookie still returns 200 and the same session id. Before this change it returned 401.
  4. POST /account/sessions/token creates a second session; GET /account/sessions lists both.
  5. Run the session flow again with the second cookie: the second session is replaced and its cookie now gets 401, so the existing behavior of createOAuth2Session is unchanged.

Existing OAuth2 e2e tests (testCreateOAuth2AccountSession, testOAuth2TokenSessionProviderAccessToken, loopback and OIDC token tests) run the token flow without a session cookie and are unaffected.

docker compose exec appwrite test tests/e2e/Services/Account --filter=OAuth2

I did not run the e2e suite locally (the only local stack on this machine is bound to a different working copy), so please rely on CI for the run. php -l and Pint pass on the touched files.

Related PRs and Issues

  • Console: appwrite/vibes feat/smtp-resend-quick-setup works around this with a guest-tolerant /auth/smtp/callback route that restores the session; with this fix the success URL can point straight back at the page that started the flow.

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs? (Only the endpoint's docs markdown changed; no route metadata or specs.)

🤖 Generated with Claude Code

The OAuth2 redirect handler deleted the caller's current session for
both flows. That is right for createOAuth2Session, which replaces it
with the session it creates and sets the cookie, so the browser stays
signed in. createOAuth2Token creates no session there; the app exchanges
the returned token through createSession later. Deleting the current
session in that flow signed the caller out until the exchange, so a
signed-in user connecting a provider landed on the success URL as a
guest and any auth guard bounced them before the token could be used.

Delete the current session only when the flow creates a replacement.
In the token flow the identity is still attached to the signed-in
account, the existing session stays valid, and createSession adds a
session next to it.

- Document the behavior on createOAuth2Token.
- E2E: token flow while signed in keeps the session, the exchange adds
  a second one, and the session flow still replaces the current one.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@greptile-apps

greptile-apps Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no outstanding correctness or security failures identified.

Summary

  • Limits current-session deletion to flows that immediately create a replacement session.
  • Documents that an existing session remains active during token exchange.
  • Adds end-to-end coverage for session preservation, token exchange, and existing session-replacement behavior.

Reviews (2) · Last reviewed commit: "test(account): follow OAuth2 redirects w..."

@github-actions

github-actions Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix-oauth2-token-flow-keeps-current-session (after).

Metric Before After Change
🚀 Requests/sec 226.41 219.21 ⚪ -3.2%
⏱️ Latency P50 77.53 ms 79.36 ms ⚪ +2.4%
⏱️ Latency P95 179.92 ms 187.61 ms ⚪ +4.3%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 79.36 187.61 13,680 219.21 +7.69
Account 154.41 283.7 720 12.11 +13.79
TablesDB 75.31 137.88 7,440 121.03 +7.22
Storage 75.88 168.85 3,600 60.59 +13.77
Functions 118.9 234.73 1,920 33.06 +11.2

Top API waits (after)

API request Max wait (ms)
account.name.update 451.8
functions.create 427.36
functions.variables.create 412.25
functions.variables.update 389.36
account.prefs.update 385.63

The helper only drives the mock provider round trip; the token and
session assertions after it establish the behavior. Asserting the
private appwrite: callback and redirect URLs coupled the test to
routing details.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

@Meldiron Meldiron left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Meldiron
Meldiron merged commit 2b7a4a8 into main Sep 10, 2026
45 of 46 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Sep 10, 2026
2 of 4 tasks
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.

2 participants