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

Đang mở
#686 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
52/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript, vite
Lĩnh vực
build-system, devtools

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
627
Fork
66
Merge trung bình
14 giờ 38 phút
Pull request đã merge (30 ngày)
179

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của voidzero-dev/void

Tất cả issue của voidzero-dev/void

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.