Skip to content

test(provider): add regression tests for AuthKitProvider initialization - #98

Open
nicknisi wants to merge 3 commits into
mainfrom
fix/issue-87
Open

nicknisi wants to merge 3 commits into
mainfrom
fix/issue-87

Conversation

@nicknisi

Copy link
Copy Markdown
Member

Summary

After createClient() resolves, the .then() callback was setting state with only isLoading: false and user, spreading from initialState which has organizationId: null. This overwrote any organizationId (and other auth metadata) that had been correctly set by the onRefresh/handleRefresh callback moments earlier due to React state batching.

The fix reads auth metadata (organizationId, role, roles, permissions, featureFlags) directly from the access token's JWT claims in the .then() callback, using the existing getClaims() helper. This ensures state is fully populated regardless of callback ordering. A try/catch handles unauthenticated sessions gracefully.

Manual reproduction steps

  1. Configure AuthKitProvider with an onRefresh callback that logs response.organizationId
  2. Sign in with a user that has an organization selected during the WorkOS login flow
  3. After redirect back, check useAuth().organizationId

On main: organizationId is null even though onRefresh received the correct value — the .then() callback overwrites it
On this branch: organizationId is correctly populated from the access token JWT claims

What was tested

Automated

3 vitest unit tests added and passing:

  1. organizationId is populated from JWT claims after createClient resolves
  2. Auth metadata (role, permissions, featureFlags) comes from token claims, not initialState defaults
  3. onRefresh-set organizationId is not overwritten by the .then() callback

Manual

  • Built SDK and linked to example app via npm link
  • Started example app, signed in through AuthKit hosted login flow
  • Verified all useAuth() fields are properly populated on the Account page
  • Independent scenario script verified: getClaims() correctly extracts org_id from JWT, provider source uses getClaims in .then() callback, old bug pattern removed, catch block handles unauthenticated sessions
  • 9 scenario assertions pass

Verification

Before

before.png

After

after.png

Issue

Closes #87

Follow-ups

  • @types/react-dom@^19 in devDependencies conflicts with @types/react@18.3.3 — version mismatch may cause type-checking issues in development (pre-existing, not introduced by this fix)
  • Single commit bundles bug fix and test infrastructure setup (vitest, testing-library, jsdom) — could be split for cleaner history but acceptable given focused scope

@nicknisi nicknisi left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Code Review Findings

Warnings

  • [Convention] @types/react-dom@^19 conflicts with @types/react@18.3.3 in devDependencies — version mismatch may cause type-checking issues in development (package.json:33)

Info

  • [Principle 10] Single commit bundles bug fix and test infrastructure setup (vitest, testing-library, jsdom). Ideally separate commits for fix vs test infra, but acceptable given focused scope.

Automated review by case/reviewer agent

@greptile-apps

greptile-apps Bot commented May 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Adds regression tests for AuthKitProvider initialization without changing production code.

  • Provider startup tests now run in CI.

Both previous findings were addressed by removing the initialization-time token read.

Reviews (2) · Last reviewed commit: "ci: run the tests" · Reviewed by Greptile

Comment thread src/provider.tsx Outdated
Comment thread src/provider.tsx Outdated

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

Add vitest (jsdom), @testing-library/react, @testing-library/dom and
@testing-library/jest-dom, plus react/react-dom 18 and @types/react-dom 18
as devDependencies so they match the pinned @types/react 18.3.3. Replace
the placeholder test script with `vitest run`.

The config is vitest.config.mts so Vite loads it as ESM in this CommonJS
package. src/__tests__ is excluded from `files` so the published package
contents stay the same.

The lockfile starts from main's and keeps main's registry-agnostic style
(no "resolved" fields). It was generated with npm 10 (Node 20, as in CI):
npm 10's `npm ci` needs the nested esbuild 0.28.2 entry for vite 8's
optional esbuild peer, which npm 11 would leave out.
Cover the init paths main's provider already handles, so they stay fixed:

- an onRefresh that fires before createClient resolves keeps its
  organization (the #87 scenario, fixed on main by spreading prev);
- an onRefresh after init still applies;
- a token without org_id clears the organization claims;
- createClient rejecting logs the error and settles signed out;
- under StrictMode double effects only the second init runs;
- a superseded init that resolves or rejects late is ignored.

Only createClient is mocked; getClaims and LoginRequiredError are the
real authkit-js exports.
@nicknisi nicknisi changed the title fix(provider): read organizationId from access token after client initialization test(provider): add regression tests for AuthKitProvider initialization Oct 21, 2026
@nicknisi

Copy link
Copy Markdown
Member Author

@greptileai review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

useAuth() returns organizationId: null after onRefresh callback receives valid organizationId

1 participant