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

Closed
opened 2026-04-15 22:16:38 -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: Closed

Base: canaryHead: feat/public-endpoints-infrastructure


📝 Commits (1)

  • 8f0116d 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 publicEndpoints so plugins can serve well-known routes at the origin root (/.well-known/*) while keeping all other endpoints under basePath. When used, the auth instance exposes a dedicated public router and API.

  • New Features

    • publicEndpoints on BetterAuthPlugin; new publicRouter mounts at /, and the main handler auto-routes /.well-known/*.
    • publicHandler and publicApi are exposed only when at least one plugin defines publicEndpoints.
    • Detects and logs conflicts for public paths and HTTP methods; types updated to include publicEndpoints, publicHandler, and publicApi.
  • Bug Fixes

    • After hooks can return void/null/undefined without modifying the response.
    • Safer per-request context for dynamic baseURL (recomputes trusted origins/providers and cookies; resolves base URL on first request for static configs).

Written for commit 8f0116d29c. 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:** ❌ Closed **Base:** `canary` ← **Head:** `feat/public-endpoints-infrastructure` --- ### 📝 Commits (1) - [`8f0116d`](https://github.com/better-auth/better-auth/commit/8f0116d29c3847c2cb6f7bb81b9096d507eecde9) 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 `publicEndpoints` so plugins can serve well-known routes at the origin root (`/.well-known/*`) while keeping all other endpoints under `basePath`. When used, the auth instance exposes a dedicated public router and API. - New Features - `publicEndpoints` on `BetterAuthPlugin`; new `publicRouter` mounts at `/`, and the main handler auto-routes `/.well-known/*`. - `publicHandler` and `publicApi` are exposed only when at least one plugin defines `publicEndpoints`. - Detects and logs conflicts for public paths and HTTP methods; types updated to include `publicEndpoints`, `publicHandler`, and `publicApi`. - Bug Fixes - After hooks can return void/null/undefined without modifying the response. - Safer per-request context for dynamic `baseURL` (recomputes trusted origins/providers and cookies; resolves base URL on first request for static configs). <sup>Written for commit 8f0116d29c3847c2cb6f7bb81b9096d507eecde9. 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-04-15 22:16:38 -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#24275