Fix client bundling and upgrade authkit-session to 0.3.0#26
Merged
Conversation
Greptile OverviewGreptile SummaryThis PR upgrades Key Changes:
Architecture: Testing: Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Client
participant Middleware
participant ServerFunction
participant AuthKit
participant Storage
Client->>Middleware: Request
Middleware->>AuthKit: getAuthkit()
AuthKit-->>Middleware: authkit instance
Middleware->>AuthKit: withAuth(request)
AuthKit-->>Middleware: { auth, refreshedSessionData }
Note over Middleware: Create pendingHeaders & context with __setPendingHeader
Middleware->>ServerFunction: args.next({ context })
alt Server function refreshes session
ServerFunction->>AuthKit: refreshSession(orgId)
AuthKit->>Storage: saveSession()
Storage->>Storage: getAuthKitContextOrNull()
alt Context available
Storage->>Middleware: __setPendingHeader(Set-Cookie, value)
else Context unavailable
Storage-->>ServerFunction: { response with headers }
end
end
ServerFunction-->>Middleware: result
alt refreshedSessionData exists
Middleware->>AuthKit: saveSession(undefined, refreshedSessionData)
AuthKit-->>Middleware: { response with Set-Cookie }
Middleware->>Middleware: Extract & append Set-Cookie to pendingHeaders
end
Middleware->>Middleware: Apply pendingHeaders to response
Middleware-->>Client: Response with session cookies
|
dandorman
approved these changes
Dec 3, 2025
|
🙏 |
Member
Author
- Create src/server/context.ts with AuthKitServerContext interface - Add getAuthKitContext() and getAuthKitContextOrNull() helpers - Remove `as any` casts from production code - Rename _setPendingHeader to __setPendingHeader (deeper internal signal)
- Use Headers API instead of Record<string,string> for pendingHeaders to properly handle multiple Set-Cookie values via .append() - Add runtime validation for both auth and request in context - Convert error message to template literal for readability - Add unit tests for context.ts (12 tests) and middleware.ts (7 tests) - Fix test mock to include request property - Standardize TanStack dependency versioning in example
TanStack Start's AsyncLocalStorage context is torn down after args.next() returns. This fixes session cookie persistence for the refresh case by: - Storage falls back to putting headers on response when context unavailable - Middleware extracts Set-Cookie from saveSession response and adds to pendingHeaders - Updated tests to cover both context-available and fallback paths
Dynamic imports were originally used to avoid bundling server-only code into the client, but with the src/server/ directory boundary working properly, static imports are sufficient and simpler.
85fbc82 to
3c43334
Compare
Merged
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
This PR upgrades
@workos/authkit-sessionto 0.3.0 and fixes critical session persistence issues.Problem: Can't Use TanStack's Built-in setResponseHeaders
TanStack Start provides
setResponseHeaders()for setting cookies from server functions. However, importing it breaks builds:This happens because TanStack's barrel exports pull in
node:streamcode that Vite can't handle. This is a known issue (TanStack/router#4022).Impact: When
refreshSession()orswitchToOrganization()is called, the session cookie can't be persisted. Users switch orgs successfully, but on page refresh they revert to their previous org.Solution: Context-Based Session Persistence
Instead of importing from
@tanstack/react-start/server, we use middleware context to defer cookie persistence:This follows the standard middleware pattern used in Express/Koa/etc.
Additional Fixes
getAuthKitContextOrNull()now gracefully handles unavailable context (e.g., after middleware completes)reloadDocumentto avoid CORS issues with external redirectsChanges
storage.ts- No TanStack server imports; uses context for headersmiddleware.ts- Passes header setter through context, applies pending headers to responseauth-helpers.ts- Returns session data for middleware to persistcontext.ts- Try-catch for graceful context accessserver-functions.ts/actions.ts- Use context-based persistenceRelated