Auth: Expose the per-request Better Auth instance to server routes

Aperta
#661 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
18/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start with the void/auth exports getUser, getSession and requireAuth, and with runVoidAuthMiddleware, which stores the per-request Better Auth instance under the Hono context key __voidAuth. The issue asks for a supported accessor such as getAuth(c) plus a decision on whether /.well-known/ OAuth discovery paths should be forwarded. Done means the maintainers have chosen an approach and a route can get the instance without the private key.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

I'd like a supported way to get the Better Auth instance that Void creates for the
current request, so that server routes can pass it to Better Auth plugin helpers.

Today void/auth exports getUser(), getSession() and requireAuth(), which cover
user and session checks, but it doesn't export the instance itself.

The only way I've found to reach the instance is to read the internal Hono context key "__voidAuth",
which isn't documented and could change in any release.

Use case

My app is an MCP server that uses Better Auth's OAuth provider, with the jwt() and
mcp() plugins from @better-auth/mcp. Several pieces of that setup need the
Better Auth instance as an argument, so I can't write them with the current
void/auth exports:

  1. The MCP route wraps its handler with requireMcpAuth(auth, handler, options) from
    @better-auth/mcp, which checks the OAuth access token on each request.
  2. MCP clients fetch OAuth discovery documents at the site root, e.g.
    /.well-known/oauth-protected-resource/mcp and
    /.well-known/oauth-authorization-server/api/auth. Void only forwards
    /api/auth/* to Better Auth, so I added a route under routes/.well-known/ that
    passes these requests to auth.handler(request).
  3. My sign-in and consent page loaders use auth.$context to count users through
    the database adapter, because the app allows only one account.

Current workaround

I read the instance from the Hono context in one helper function, and every route
calls that helper:

import type { Context } from "hono";
import type { betterAuth } from "better-auth";

type AuthInstance = ReturnType<typeof betterAuth>;

export function getAuthInstance(c: Context): AuthInstance {
  const auth = c.get("__voidAuth") as AuthInstance | undefined;
  if (!auth) {
    throw new Error('Void did not provide a Better Auth instance at "__voidAuth".');
  }
  return auth;
}

The workaround depends on runVoidAuthMiddleware, which calls
c.set("__voidAuth", auth) in Void's Better Auth runtime code. I checked that the key
is the same in 1.2.1 and 1.4.0. If the key is renamed, my MCP endpoint, the discovery
documents and the sign-in page all stop working, and the type cast means nothing
catches it at build time.

Proposal

I'd like void/auth to export a function that returns the current request's
instance, with the same request handling as requireAuth(c). For example:

import { getAuth } from "void/auth";

export const POST = defineHandler(async (c) => {
  const auth = getAuth(c);
  return requireMcpAuth(auth, handleMcpRequest, { resource })(c.req.raw);
});

I'd also like the return type to include the plugins from auth.ts, the same way
AuthUser and AuthSession now include plugin fields in 1.4.0, so that the
endpoints plugins add under auth.api type check without a cast.

If Void prefers not to expose the instance, I could instead use built-in support for
the two OAuth needs above. That would mean forwarding the /.well-known/ OAuth
discovery paths to Better Auth when the mcp() or oauthProvider() plugin is
enabled, and adding a documented way to protect a route with an OAuth access token.

Environment

  • void 1.4.0, @void/svelte 1.4.0
  • better-auth 1.7.12, @better-auth/mcp 1.7.12
  • Native Cloudflare deploy (Workers and D1)

Disclaimers

Written by Claude Opus 5, reviewed and edited by Humans.

Lingua principale
TypeScript
Stelle
627
Fork
66
Merge medio
14h 17m
PR unite (30g)
200

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di voidzero-dev/void

Tutte le issue di voidzero-dev/void

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.