Skip to content

Conversation

@nicknisi
Copy link
Member

@nicknisi nicknisi commented Sep 9, 2025

Summary

  • Add eagerAuth middleware option for synchronous access token availability
  • Implement 30-second JWT cookie mechanism for third-party service integration
  • Add comprehensive test coverage (100% for tokenStore and cookie modules)
  • Include security documentation and best practices
  • Fix cookie name consistency across codebase

Key Changes

  • New eagerAuth option: Enables synchronous token access on initial page load
  • JWT cookie mechanism: 30-second TTL cookie automatically consumed by client
  • Request type detection: Only sets cookies on document requests, not API/RSC/prefetch

Usage

To opt-in to this feature, add the following in middleware.ts:

import { authkitMiddleware } from "@workos-inc/authkit-nextjs";

export default authkitMiddleware({
  eagerAuth: true
});

With this enabled, the token will now be immediately accessible via the useAccessToken hook on the client.

capture_20250909_155642

Without that, the first couple of renders will not have the accessToken available until it is asynchronously loaded.

capture_20250909_155729 2

Fixes #286

@nicknisi nicknisi requested a review from a team as a code owner September 9, 2025 20:54
@nicknisi nicknisi requested a review from amygdalama September 9, 2025 20:54
return;
}

const pattern = new RegExp(`${this.escapeRegExp(jwtCookieName)}=([^;]+)`);
Copy link
Contributor

Choose a reason for hiding this comment

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

i'm slightly uncomfortable that we're having to string interpolate a regexp here... can we do some light-weight parsing or something instead?

Copy link
Contributor

@dandorman dandorman left a comment

Choose a reason for hiding this comment

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

Nice work! (and very thorough job on the tests)

@nicknisi nicknisi merged commit 2637445 into main Sep 11, 2025
4 checks passed
@nicknisi nicknisi deleted the nicknisi/eager-token-access branch September 11, 2025 21:17
@nicknisi nicknisi mentioned this pull request Sep 11, 2025
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.

Passing accessToken from server to client

5 participants