Skip to content

One session module for the extension and the iPhone app; the popup and background no longer race a refresh - #119

Merged
NiallJoeMaher merged 2 commits into
mainfrom
arch-session
Oct 27, 2026
Merged

NiallJoeMaher merged 2 commits into
mainfrom
arch-session

Conversation

@NiallJoeMaher

Copy link
Copy Markdown
Contributor

createSession(store) in packages/client now holds the token refresh, isSignedIn and the revoke, and register holds dynamic registration. The extension (browser storage plus a Web Lock) and the iPhone app (Keychain) each pass a store. The platform-specific sign-in stays in each app.

The race was real. I ran the real Better Auth handler (mcp(), whose refreshTokenReuseInterval defaults to 30 s) in a throwaway vitest:

  • Two concurrent refreshes with the same token: one got 200 and the other 400 invalid_grant, 5 out of 5 times.
  • A reuse after the first refresh had finished, within 30 s: the server replayed the same pair (200).

Only the in-flight case hurts. The extension's guard lived in memory, so the popup and the service worker could each spend the token. The loser then wrote tokens: null and signed the person out.

Fix

  • A Web Lock (navigator.locks) around the refresh. The popup, options page and worker all share it.
  • The store is read again under the lock, so a context that arrives second uses the tokens already stored.
  • A refused refresh counts as success if the stored refresh token changed in the meantime. This covers the iPhone's Swift share extension, which cannot share the lock.

Tests (packages/client/src/client.test.ts): two session "contexts" share one store against a fake rotating token endpoint. Before the fix they returned ['Bearer a1', null] (red); now both stay signed in and the token is spent once. Further tests cover a refusal where another app refreshed first, a refused token (signs out) and offline (keeps the tokens).

Also: the extension uses pkcePair instead of hand-rolled PKCE, and the popup and badge ask isSignedIn() instead of reading raw storage keys.

Not done: PluckClient.swift is unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Per4f8yL5A7nL9frZVDJj7

@vercel

vercel Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
pluck Ready Ready Preview Oct 27, 2026 7:32am UTC

Request Review

NiallJoeMaher and others added 2 commits October 27, 2026 07:28
… background refreshing at once no longer sign you out

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Per4f8yL5A7nL9frZVDJj7
Signed-off-by: NiallJoeMaher <nialljoemaher@gmail.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Per4f8yL5A7nL9frZVDJj7
Signed-off-by: NiallJoeMaher <nialljoemaher@gmail.com>
@NiallJoeMaher
NiallJoeMaher merged commit 4bba966 into main Oct 27, 2026
1 of 5 checks passed
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.

1 participant