Skip to content

Conversation

@himself65
Copy link
Member

@himself65 himself65 commented Sep 26, 2025

Move it on top of Auth to reduce some deeper type inference.

Reduce perf from ~9.5s to ~8.7s

image

Summary by cubic

Made Auth generic and plugin-aware to provide precise typings for api, options, $Infer, and merged $ERROR_CODES. Added type tests to validate default Auth and plugin error code merging.

  • Refactors
    • Introduced Auth and updated betterAuth to return Auth.
    • api now derives from router endpoints via FilterActions; removed InferAPI casting.
    • $ERROR_CODES types merge InferPluginErrorCodes with BASE_ERROR_CODES.
    • $Infer exposes session/user inferred from Options and includes plugin types.
    • Added vitest type tests for default Auth and custom plugin $ERROR_CODES.
@himself65 himself65 requested a review from Bekacru as a code owner September 26, 2025 20:53
@vercel
Copy link

vercel bot commented Sep 26, 2025

@himself65 is attempting to deploy a commit to the better-auth Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Sep 26, 2025

Open in StackBlitz

better-auth

npm i https://pkg.pr.new/better-auth/better-auth@4930 

@better-auth/cli

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/cli@4930 

@better-auth/core

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/core@4930 

@better-auth/expo

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/expo@4930 

@better-auth/sso

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/sso@4930 

@better-auth/stripe

npm i https://pkg.pr.new/better-auth/better-auth/@better-auth/stripe@4930 

commit: 286f881

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@himself65 himself65 changed the title feat: improve type Auth perf: improve type Auth Sep 26, 2025
@himself65 himself65 added this pull request to the merge queue Sep 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 26, 2025
@himself65 himself65 added this pull request to the merge queue Sep 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 26, 2025
@himself65 himself65 enabled auto-merge September 26, 2025 22:28
@himself65 himself65 added this pull request to the merge queue Sep 26, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 26, 2025
@himself65 himself65 added this pull request to the merge queue Sep 27, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 27, 2025
@himself65 himself65 added this pull request to the merge queue Sep 27, 2025
Merged via the queue into better-auth:canary with commit 350ccfd Sep 27, 2025
10 of 15 checks passed
@artimath
Copy link

artimath commented Sep 27, 2025

So, I still had the same issues with this version, but then codex figured it out... somehow. Not sure if it will crash during runtime, but it did resolve the types cleanly WHILE exactOptionalPropertyTypes is true

here is a PR/branch/commit with the fix: artimath/tanstack-effect-cloudflare-template#1

here is the code that changed it, adding $InferAuth to the createAuthClient options:

import { adminClient, emailOTPClient, inferAdditionalFields, magicLinkClient, multiSessionClient, organizationClient, passkeyClient, twoFactorClient, } from "better-auth/client/plugins"; import { InferAuth } from "better-auth/client"; import { createAuthClient } from "better-auth/react"; import { env } from "@/lib/env.client"; import { ac, admin as adminRole, superadmin as superAdminRole, user as userRole } from "./permissions"; import type { auth } from "@/lib/auth/auth"; export const authClient = createAuthClient({ baseURL: env.VITE_SERVER_URL, plugins: [ inferAdditionalFields<typeof auth>(), twoFactorClient(), passkeyClient(), adminClient({ ac, roles: { user: userRole, admin: adminRole, superadmin: superAdminRole, }, }), organizationClient(), emailOTPClient(), magicLinkClient(), multiSessionClient(), ], $InferAuth: InferAuth<typeof auth>(), <--- magic fix? }); export type AuthClient = typeof authClient; export type Session = AuthClient["$Infer"]["Session"]; export type SessionUser = Session["user"]; export type Organization = AuthClient["$Infer"]["Organization"]; export type ActiveOrganization = AuthClient["$Infer"]["ActiveOrganization"];

maybe it's a mirage, but on the off-chance it isn't, i want to share it since i saw a lot of people running into it

@himself65 thank you for creating this and tending to it

himself65 added a commit that referenced this pull request Sep 30, 2025
himself65 added a commit that referenced this pull request Sep 30, 2025
himself65 added a commit that referenced this pull request Sep 30, 2025
ping-maxwell pushed a commit to ping-maxwell/better-auth that referenced this pull request Oct 26, 2025
ping-maxwell pushed a commit to ping-maxwell/better-auth that referenced this pull request Nov 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants