Dev server: plugins importing better-auth/api get a second copy of Better Auth's request state, so getOAuthState() is null in a plugin's callback hook

Aberta
#686 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
52/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
typescript, vite

Direção de pesquisa

Start by tracing how the void_worker environment configures Vite dependency optimization and how Void loads Better Auth at runtime; the report identifies defineRequestState in @better-auth/core as the source of the module-local ref. Reproduce with the described OAuth callback and compare against the better-auth exclusion in vite.config.ts. Done means the callback hook and Void runtime share request state in dev; verify the reported reproduction and relevant tests.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

Void 1.5.0, better-auth 1.7.15, vite 8.3.5, @cloudflare/vite-plugin 2.0.0-beta.v.10, sqlite dialect, void dev.

A Better Auth plugin added in auth.ts registers an after hook on /callback/* and calls getOAuthState() from better-auth/api to read serverContext that the same plugin set with addOAuthServerContext() before calling signInSocial. On the dev server the hook gets null, although the state row in the verification table holds the serverContext and ctx.context.newSession is set. The same plugin works with betterAuth() used directly, outside Void.

Cause, as far as we can tell: Better Auth keys request state by a module-local ref (defineRequestState in @better-auth/core creates const ref = Object.freeze({}) per call; only the AsyncLocalStorage is on globalThis). The worker environment's optimizeDeps excludes void, so Void's runtime (which creates the auth instance and runs the callback) and user code importing better-auth/api (pre-bundled by Vite) load different instances of better-auth/dist/api/state/oauth.mjs. The callback's setOAuthState and the plugin's getOAuthState use different refs. Void already works around the same class of duplicate for its own auth ALS (authContext on a Symbol.for key).

Workaround: environments: { void_worker: { optimizeDeps: { exclude: ['better-auth'] } } } in vite.config.ts, after which the hook sees the state. A production build bundles one copy and is not affected.

Expected: user code and Void's runtime share one instance of better-auth on the dev server, for example by pre-bundling better-auth with Void's runtime, or by Void excluding better-auth along with itself.

Repro: a plugin with addOAuthServerContext({ x: 1 }) + signInSocial()(…) in a GET endpoint, and an after hook on /callback/ logging (await getOAuthState())?.serverContext. Run the flow against any OAuth provider (for example oauth2-mock-server through genericOAuth). It logs null under void dev and { x: 1 } with the exclusion above.

Linguagem predominante
TypeScript
Estrelas
627
Forks
66
Merge médio
14h 38min
PRs com merge (30d)
179

Preparar o ambiente

Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de voidzero-dev/void

Todas as issues de voidzero-dev/void

Issues semelhantes

Mais issues de TypeScript

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.