[PR #5723] fix(jwt): creates a jwk on plugin init #6180

Open
opened 2026-03-13 12:50:16 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5723
Author: @LightTab2
Created: 11/2/2025
Status: 🔄 Open

Base: canaryHead: jwt/jwksRace


📝 Commits (6)

  • 00e6666 fix: creates a key pair if the database is empty
  • d8b1ae0 Merge branch 'canary' into jwt/jwksRace
  • cd53ce5 fix: throws error if key does not exist
  • bff33fd fix: create key the proper way
  • fc264b7 lint
  • 0e26c3c fix: import

📊 Changes

3 files changed (+39 additions, -21 deletions)

View changed files

📝 packages/better-auth/src/plugins/jwt/index.ts (+25 -7)
📝 packages/better-auth/src/plugins/jwt/sign.ts (+2 -4)
📝 packages/better-auth/src/test-utils/test-instance.ts (+12 -10)

📄 Description

Fixes #5663 by thinking outside the box.

We can check if the jwks table is empty when the plugin is being initialized and add the key pair if needed. Endpoints should not be exposed yet, so this prevents the race condition.

The tests had to be adjusted to run migrations before auth initialization.

Makes #5684 redundant.


Summary by cubic

Prevents the JWKS race by creating and storing a key pair during JWT plugin initialization when the jwks table is empty. Also removes lazy key creation and runs migrations before booting the test instance.

  • Bug Fixes
    • On plugin init, generate and persist a JWK if none exists; throw if a key is missing at runtime.
    • In test utils, run migrations before creating the auth instance (non-MongoDB).

Written for commit 0e26c3c48a. Summary will update automatically on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/5723 **Author:** [@LightTab2](https://github.com/LightTab2) **Created:** 11/2/2025 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `jwt/jwksRace` --- ### 📝 Commits (6) - [`00e6666`](https://github.com/better-auth/better-auth/commit/00e6666a0c466467bb8fde3e8ccc12991140f605) fix: creates a key pair if the database is empty - [`d8b1ae0`](https://github.com/better-auth/better-auth/commit/d8b1ae0ed460a21853d12fcdcaa402fd59296dda) Merge branch 'canary' into jwt/jwksRace - [`cd53ce5`](https://github.com/better-auth/better-auth/commit/cd53ce54b8f15e3fc7793079a9c61ebf6e710191) fix: throws error if key does not exist - [`bff33fd`](https://github.com/better-auth/better-auth/commit/bff33fd1f1b614e32dfe967d5d699dac943c548e) fix: create key the proper way - [`fc264b7`](https://github.com/better-auth/better-auth/commit/fc264b7b2b7947d02bc649b44e208c66484ab9c8) lint - [`0e26c3c`](https://github.com/better-auth/better-auth/commit/0e26c3c48ac558ead8b239ff99b1ee1ebb0bd2ec) fix: import ### 📊 Changes **3 files changed** (+39 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/jwt/index.ts` (+25 -7) 📝 `packages/better-auth/src/plugins/jwt/sign.ts` (+2 -4) 📝 `packages/better-auth/src/test-utils/test-instance.ts` (+12 -10) </details> ### 📄 Description Fixes #5663 by thinking outside the box. We can check if the `jwks` table is empty when the plugin is being initialized and add the key pair if needed. Endpoints should not be exposed yet, so this prevents the race condition. The tests had to be adjusted to run migrations before auth initialization. Makes #5684 redundant. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevents the JWKS race by creating and storing a key pair during JWT plugin initialization when the jwks table is empty. Also removes lazy key creation and runs migrations before booting the test instance. - **Bug Fixes** - On plugin init, generate and persist a JWK if none exists; throw if a key is missing at runtime. - In test utils, run migrations before creating the auth instance (non-MongoDB). <sup>Written for commit 0e26c3c48ac558ead8b239ff99b1ee1ebb0bd2ec. Summary will update automatically on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:50:16 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#6180