[PR #7524] feat(core): add publicEndpoints plugin capability for well-known endpoints #7374

Open
opened 2026-03-13 13:33:51 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7524
Author: @gustavovalverde
Created: 1/21/2026
Status: 🔄 Open

Base: canaryHead: feat/public-endpoints-infrastructure


📝 Commits (1)

  • 737971a feat(core): add publicEndpoints plugin capability for well-known endpoints

📊 Changes

6 files changed (+507 additions, -91 deletions)

View changed files

📝 packages/better-auth/src/api/index.ts (+129 -19)
packages/better-auth/src/api/public-endpoints.test.ts (+270 -0)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+3 -0)
📝 packages/better-auth/src/auth/base.ts (+93 -71)
📝 packages/better-auth/src/types/auth.ts (+3 -1)
📝 packages/core/src/types/plugin.ts (+9 -0)

📄 Description

Summary

Adds publicEndpoints plugin capability for RFC 8414 / OIDC Discovery compliance.

This allows plugins to register endpoints at the origin root (e.g., /.well-known/openid-configuration) instead of under the basePath (e.g., /api/auth/.well-known/...).

Important clarification: This does NOT mount Better Auth at root. Only /.well-known/* paths are intercepted by the main handler and routed to the public router. All other endpoints remain at the configured basePath.

Closes #7453

Changes

  • Add publicEndpoints field to BetterAuthPlugin type
  • Add publicRouter that mounts at / instead of basePath
  • Auto-route /.well-known/* in main handler to publicRouter
  • Export optional publicHandler and publicApi on auth object
  • Add conflict detection for public endpoints

How It Works

  1. Plugins can define publicEndpoints alongside regular endpoints
  2. The main handler automatically routes /.well-known/* requests to the public router
  3. publicHandler and publicApi are only defined when plugins actually use publicEndpoints
  4. Existing behavior is completely unchanged - no breaking changes

Example Usage

const myPlugin = {
  id: "my-plugin",
  // Regular endpoints at basePath (e.g., /api/auth/my-endpoint)
  endpoints: {
    myEndpoint: createAuthEndpoint("/my-endpoint", ...)
  },
  // Public endpoints at root (e.g., /.well-known/my-config)  
  publicEndpoints: {
    wellKnownConfig: createAuthEndpoint("/.well-known/my-config", ...)
  }
};

Test Plan

  • New tests for publicEndpoints functionality (11 tests)
  • Existing to-auth-endpoints tests pass (34 tests)
  • Existing check-endpoint-conflicts tests pass (13 tests)
  • TypeScript types check
  • Lint passes

Summary by cubic

Adds a publicEndpoints capability so plugins can serve well-known endpoints at the origin root (/.well-known/...) for RFC 8414 and OIDC Discovery. The main handler now routes only /.well-known/* to a public router and exposes publicHandler/publicApi when used.

  • New Features

    • Added publicEndpoints to BetterAuthPlugin and a publicRouter at "/"; main handler auto-routes only /.well-known/*; expose publicHandler and publicApi when present.
    • Detects conflicts for public paths and logs combined conflicts for regular and public endpoints.
  • Bug Fixes

    • After hooks can return void/null/undefined without changing the response.
    • Safer per-request context for dynamic baseURL to compute trusted origins and cookies correctly.

Written for commit 737971aace378ffd0f23f7b0b9fe819e2f0faaff. Summary will update 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/7524 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 1/21/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `feat/public-endpoints-infrastructure` --- ### 📝 Commits (1) - [`737971a`](https://github.com/better-auth/better-auth/commit/737971aace378ffd0f23f7b0b9fe819e2f0faaff) feat(core): add publicEndpoints plugin capability for well-known endpoints ### 📊 Changes **6 files changed** (+507 additions, -91 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/index.ts` (+129 -19) ➕ `packages/better-auth/src/api/public-endpoints.test.ts` (+270 -0) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+3 -0) 📝 `packages/better-auth/src/auth/base.ts` (+93 -71) 📝 `packages/better-auth/src/types/auth.ts` (+3 -1) 📝 `packages/core/src/types/plugin.ts` (+9 -0) </details> ### 📄 Description ## Summary Adds `publicEndpoints` plugin capability for RFC 8414 / OIDC Discovery compliance. This allows plugins to register endpoints at the origin root (e.g., `/.well-known/openid-configuration`) instead of under the `basePath` (e.g., `/api/auth/.well-known/...`). **Important clarification**: This does NOT mount Better Auth at root. Only `/.well-known/*` paths are intercepted by the main handler and routed to the public router. All other endpoints remain at the configured `basePath`. Closes #7453 ## Changes - Add `publicEndpoints` field to `BetterAuthPlugin` type - Add `publicRouter` that mounts at `/` instead of basePath - Auto-route `/.well-known/*` in main handler to publicRouter - Export optional `publicHandler` and `publicApi` on auth object - Add conflict detection for public endpoints ## How It Works 1. Plugins can define `publicEndpoints` alongside regular `endpoints` 2. The main `handler` automatically routes `/.well-known/*` requests to the public router 3. `publicHandler` and `publicApi` are only defined when plugins actually use `publicEndpoints` 4. Existing behavior is completely unchanged - no breaking changes ## Example Usage ```typescript const myPlugin = { id: "my-plugin", // Regular endpoints at basePath (e.g., /api/auth/my-endpoint) endpoints: { myEndpoint: createAuthEndpoint("/my-endpoint", ...) }, // Public endpoints at root (e.g., /.well-known/my-config) publicEndpoints: { wellKnownConfig: createAuthEndpoint("/.well-known/my-config", ...) } }; ``` ## Test Plan - [x] New tests for publicEndpoints functionality (11 tests) - [x] Existing to-auth-endpoints tests pass (34 tests) - [x] Existing check-endpoint-conflicts tests pass (13 tests) - [x] TypeScript types check - [x] Lint passes <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Adds a `publicEndpoints` capability so plugins can serve well-known endpoints at the origin root (`/.well-known/...`) for RFC 8414 and OIDC Discovery. The main handler now routes only `/.well-known/*` to a public router and exposes `publicHandler`/`publicApi` when used. - **New Features** - Added `publicEndpoints` to `BetterAuthPlugin` and a `publicRouter` at "/"; main handler auto-routes only `/.well-known/*`; expose `publicHandler` and `publicApi` when present. - Detects conflicts for public paths and logs combined conflicts for regular and public endpoints. - **Bug Fixes** - After hooks can return void/null/undefined without changing the response. - Safer per-request context for dynamic `baseURL` to compute trusted origins and cookies correctly. <sup>Written for commit 737971aace378ffd0f23f7b0b9fe819e2f0faaff. Summary will update 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 13:33:51 -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#7374