[GH-ISSUE #980] authClient.$ERROR_CODES's key behaves as a function #25854

Closed
opened 2026-04-17 16:08:25 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @aiduorin on GitHub (Dec 21, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/980

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. create a nextjs app, and setup better-auth follow the docs.

  2. in src/lib/auth-client.ts, add code like below:

import { createAuthClient } from "better-auth/react"

export const authClient = createAuthClient()

console.log(typeof authClient.$ERROR_CODES.USER_ALREADY_EXISTS) // function

export const myCustomErrorCodes = {
	[authClient.$ERROR_CODES.USER_ALREADY_EXISTS]: {
		en: "User already exists",
		es: "El usuario ya existe",
	},
};
  1. import auth-client.ts, and use it:
const { data } = authClient.useSession();

Current vs. Expected behavior

I get a error:
2024-12-21_20-55

Expected behavior:

Hope to access error code as a string which was shown in docs

What version of Better Auth are you using?

v1.1.1

Provide environment information

os: archlinux x86_64 Linux 6.12.4-arch1-1
browser: chrome

Which area(s) are affected? (Select all that apply)

Client

Auth config (if applicable)

import { betterAuth } from "better-auth";
import Database from "better-sqlite3";

export const auth = betterAuth({
    database: new Database("./sqlite.db"),
})

Additional context

No response

Originally created by @aiduorin on GitHub (Dec 21, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/980 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. create a nextjs app, and setup better-auth follow the docs. 2. in src/lib/auth-client.ts, add code like below: ```ts import { createAuthClient } from "better-auth/react" export const authClient = createAuthClient() console.log(typeof authClient.$ERROR_CODES.USER_ALREADY_EXISTS) // function export const myCustomErrorCodes = { [authClient.$ERROR_CODES.USER_ALREADY_EXISTS]: { en: "User already exists", es: "El usuario ya existe", }, }; ``` 3. import auth-client.ts, and use it: ```ts const { data } = authClient.useSession(); ``` ### Current vs. Expected behavior I get a error: ![2024-12-21_20-55](https://github.com/user-attachments/assets/34b98215-efea-4046-a9e9-da889016f90e) Expected behavior: Hope to access error code as a string which was shown in docs ### What version of Better Auth are you using? v1.1.1 ### Provide environment information ```bash os: archlinux x86_64 Linux 6.12.4-arch1-1 browser: chrome ``` ### Which area(s) are affected? (Select all that apply) Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth"; import Database from "better-sqlite3"; export const auth = betterAuth({ database: new Database("./sqlite.db"), }) ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-17 16:08:25 -05:00
Author
Owner

@Bekacru commented on GitHub (Dec 21, 2024):

for now use it as a type definition.

const errorCodes = {
  //
} as Recorc<keyof typeof authClient.$ERROR_CODES, {
    en: string,
    es: string
}>

and then you can use error.code to index

const res = await authClient.signIn.email()

if(res.error.code){
    errorCodes[res.error.code]
}
<!-- gh-comment-id:2558228193 --> @Bekacru commented on GitHub (Dec 21, 2024): for now use it as a type definition. ```ts const errorCodes = { // } as Recorc<keyof typeof authClient.$ERROR_CODES, { en: string, es: string }> ``` and then you can use `error.code` to index ```ts const res = await authClient.signIn.email() if(res.error.code){ errorCodes[res.error.code] } ```
Author
Owner

@tomerzcod7 commented on GitHub (Jan 14, 2025):

Happens to me as well. not sure how to fix this

<!-- gh-comment-id:2590317491 --> @tomerzcod7 commented on GitHub (Jan 14, 2025): Happens to me as well. not sure how to fix this
Author
Owner

@tomerzcod7 commented on GitHub (Jan 14, 2025):

const errorMessages = {
            [authClient.$ERROR_CODES.USER_ALREADY_EXISTS]:
              'Email already exists',
};

I get the same error message even if I cast it with as.

Also, since those error codes come from the authClient which should only be used in the browser, this breaks SSR for me in remix :\

<!-- gh-comment-id:2590323847 --> @tomerzcod7 commented on GitHub (Jan 14, 2025): ``` const errorMessages = { [authClient.$ERROR_CODES.USER_ALREADY_EXISTS]: 'Email already exists', }; ``` I get the same error message even if I cast it with as. Also, since those error codes come from the authClient which should only be used in the browser, this breaks SSR for me in remix :\
Author
Owner

@Jackman3005 commented on GitHub (Sep 3, 2025):

Hey, I see there was a PR created and then closed to help with this. What's the way to do this right now? I can't compare codes:

if (error.code === betterAuthClient.$ERROR_CODES.EMAIL_NOT_VERIFIED) {
...
}

Which feels like the intuitive use for exposed error codes...

<!-- gh-comment-id:3247774211 --> @Jackman3005 commented on GitHub (Sep 3, 2025): Hey, I see there was a PR created and then closed to help with this. What's the way to do this right now? I can't compare codes: ```typescript if (error.code === betterAuthClient.$ERROR_CODES.EMAIL_NOT_VERIFIED) { ... } ``` Which feels like the intuitive use for exposed error codes...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#25854