feat(auth): preserve plugin/session type inference via optional plugin-tuple argument - #888
Merged
Merged
Conversation
…Options and createAuth buildBetterAuthOptions() and createAuth() now accept an optional plugin-tuple argument — the same betterAuthPlugins array passed to authPlugin() — so the returned options/Auth type carries the literal tuple instead of the widened BetterAuthOptions/Auth<BetterAuthOptions>, letting betterAuth() re-infer plugin endpoints (e.g. emailOTP()) and a customSession() plugin's replaced session shape. The supplied tuple is verified against the resolved plugin array by instance identity and order, throwing on mismatch. No-argument calls are unchanged. Also types betterAuthPlugins as better-auth's own BetterAuthPlugin[] instead of any[]. Fixes eslint's base no-redeclare rule flagging legitimate TS function overloads (switches to the overload-aware @typescript-eslint/no-redeclare). Closes #876 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FiWn5P7wPEycHS4WeQYDjs
🦋 Changeset detectedLatest commit: 455f358 The changes in this PR will be included in the next version bump. This PR includes changesets to release 9 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
Coverage Report for Core Package Coverage (./packages/core)
File CoverageNo changed files found. |
Contributor
Coverage Report for UI Package Coverage (./packages/ui)
File CoverageNo changed files found. |
Contributor
Coverage Report for CLI Package Coverage (./packages/cli)
File CoverageNo changed files found. |
Contributor
Coverage Report for Auth Package Coverage (./packages/auth)
File Coverage
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Contributor
Coverage Report for Storage Package Coverage (./packages/storage)
File CoverageNo changed files found. |
Contributor
Coverage Report for RAG Package Coverage (./packages/rag)
File Coverage
|
||||||||||||||||||||||||||||||||||||||
Contributor
Coverage Report for Storage S3 Package Coverage (./packages/storage-s3)
File CoverageNo changed files found. |
Contributor
Coverage Report for Storage Vercel Package Coverage (./packages/storage-vercel)
File CoverageNo changed files found. |
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
buildBetterAuthOptions()andcreateAuth()now accept an optional third argument — the app's ownbetterAuthPluginsarray, the exact same array passed toauthPlugin({ betterAuthPlugins })— so the returned options/Authtype carries the literal plugin tuple instead of the widenedBetterAuthOptions/Auth<BetterAuthOptions>. This letsbetterAuth()re-infer plugin-derivedauth.api.*endpoints (e.g.emailOTP()'ssignInEmailOTP) and acustomSession()plugin's replaced session shape, which were previously erased when constructing from the widened return.authPlugin({ betterAuthPlugins })by instance identity and order, throwing a clear, prefixed error naming the mismatch if they diverge — closing the drift hole the issue's original hand-rolled workaround left open.AuthConfig['betterAuthPlugins']/NormalizedAuthConfig['betterAuthPlugins']are now typed as better-auth's ownBetterAuthPlugin[]instead ofany[], with theeslint-disableremoved. Two downstream local type approximations inschema-converter.ts(modelName,type,onDelete) were widened to match better-auth's real (looser) shapes, which this type change surfaced.createAuth()'s JSDoc documents an existing, unrelated limitation of its lazyProxy: non-function properties are surfaced throughasyncwrappers, so it doesn't behave identically to a realAuthinstance — pointing typedauth.api.*reads atbuildBetterAuthOptions()+betterAuth()instead.no-redeclaredoesn't understand TypeScript function overload signatures (this PR's first use of them in this repo) — switched to the overload-aware@typescript-eslint/no-redeclareand removed now-stale disable comments inpackages/rag/src/runtime/embeddings.ts.buildBetterAuthOptions()/createAuth()JSDoc,packages/auth/CLAUDE.md, anddocs/content/reference/auth.mdto show the tuple-carrying form and state which entry point to reach for when readingauth.api.*in typed code.Test plan
packages/auth/src/server/build-better-auth-options.test.ts, colocated insrc/sotsc/pnpm buildgates it) constructs the builder's return type with a plugin tuple containingemailOTP()and acustomSession()that replaces the session shape, assertingapi.signInEmailOTP,api.sendVerificationOTP,api.checkVerificationOTP, and thecustomSessionreturn type ofapi.getSession()are present and correctly typedBetterAuthOptions(back-compat)tsc --noEmitfails, then revertingpackages/auth/tests/server.test.ts) cover: mismatched tuple length/order/identity throws naming the index; matching tuple succeeds; exactly onenextCookies()is appended, last, in both the argument and no-argument forms;createAuthwith a plugin tuple parities withbuildBetterAuthOptionsfor the same tuple;createAuthrejects when its tuple doesn't matchbuildBetterAuthOptions/createAuthparity test still passespnpm lint,pnpm format,pnpm manypkg fixpass repo-widepackages/authtest suite passes (207 tests, 12 files) andtsc --noEmitis clean@opensaas/stack-auth(minor)Closes #876
🤖 Generated with Claude Code
https://claude.ai/code/session_01FiWn5P7wPEycHS4WeQYDjs
Generated by Claude Code