[GH-ISSUE #915] CORS Issue #8497

Closed
opened 2026-04-13 03:34:41 -05:00 by GiteaMirror · 5 comments
Owner

Originally created by @spencer12162024 on GitHub (Dec 16, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/915

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create express node backend and install better-auth

  2. Setup express
    const app = express(); const port = 3006; app.use(cors({ origin: 'http://localhost:3000', exposedHeaders: ['set-auth-token'], allowedHeaders: "*" })); app.all("/api/auth/*", toNodeHandler(auth));

  3. Create frontend with nextjs

  4. Setup auth client
    export const authClient = createAuthClient({ baseURL: "http://localhost:3006", fetchOptions: { auth: { type: "Bearer", token: () => localStorage.getItem("bearer_token") || "" }, mode: "cors", }, plugins: [ twoFactorClient({ onTwoFactorRedirect() { console.log("Two factor redirect") } }), organizationClient(), adminClient() ] })

  5. Make login request
    const { data, error } = await authClient.signIn.email({ email: values.email, password: values.password, })

Note:

  • Backend and frontend are on different domains.
  • If I use fetch to make a POST request to the signIn endpoint, there is no CORS error.

Current vs. Expected behavior

Current: CORS Error
image

Expect: NO CORS Error

What version of Better Auth are you using?

1.0.17

Provide environment information

- OS: Window 11
- Browser: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"

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

Backend, Client

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @spencer12162024 on GitHub (Dec 16, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/915 ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce 1. Create express node backend and install better-auth 2. Setup express `const app = express(); const port = 3006; app.use(cors({ origin: 'http://localhost:3000', exposedHeaders: ['set-auth-token'], allowedHeaders: "*" })); app.all("/api/auth/*", toNodeHandler(auth));` 3. Create frontend with nextjs 4. Setup auth client `export const authClient = createAuthClient({ baseURL: "http://localhost:3006", fetchOptions: { auth: { type: "Bearer", token: () => localStorage.getItem("bearer_token") || "" }, mode: "cors", }, plugins: [ twoFactorClient({ onTwoFactorRedirect() { console.log("Two factor redirect") } }), organizationClient(), adminClient() ] })` 5. Make login request `const { data, error } = await authClient.signIn.email({ email: values.email, password: values.password, })` _**Note:**_ - Backend and frontend are on different domains. - If I use fetch to make a POST request to the signIn endpoint, there is no CORS error. ### Current vs. Expected behavior Current: CORS Error ![image](https://github.com/user-attachments/assets/5ab83024-15c7-4f5c-8456-83d1c2982a57) Expect: NO CORS Error ### What version of Better Auth are you using? 1.0.17 ### Provide environment information ```bash - OS: Window 11 - Browser: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24" ``` ### Which area(s) are affected? (Select all that apply) Backend, Client ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-13 03:34:41 -05:00
Author
Owner

@flrngel commented on GitHub (Dec 19, 2024):

had a same issue, solved by backdating a dependency

better-call@0.2.2

<!-- gh-comment-id:2553386353 --> @flrngel commented on GitHub (Dec 19, 2024): had a same issue, solved by backdating a dependency `better-call@0.2.2`
Author
Owner

@akshpnwr commented on GitHub (Dec 22, 2024):

I am also facing the same issue!

<!-- gh-comment-id:2558412223 --> @akshpnwr commented on GitHub (Dec 22, 2024): I am also facing the same issue!
Author
Owner

@ilyaLibin commented on GitHub (Dec 23, 2024):

had a same issue, solved by backdating a dependency

better-call@0.2.2

On FE or BE?

<!-- gh-comment-id:2559587791 --> @ilyaLibin commented on GitHub (Dec 23, 2024): > had a same issue, solved by backdating a dependency > > `better-call@0.2.2` On FE or BE?
Author
Owner

@BayBreezy commented on GitHub (Dec 24, 2024):

I am not able to use the client to make any request to the api.
I get all CORS error.

If i send a request to the endpoint directly(without using the authClient), I get no CORS error

<!-- gh-comment-id:2561472103 --> @BayBreezy commented on GitHub (Dec 24, 2024): I am not able to use the client to make any request to the api. I get all CORS error. If i send a request to the endpoint directly(without using the authClient), I get no CORS error
Author
Owner

@ilyaLibin commented on GitHub (Dec 26, 2024):

I have run the server directly with ts-node and it worked. It turned out that the cors error wasn't coming from better-auth but from serverless framework layer on top of that. fine tuning the configuration there solved the problem. If you run your app in docker or any other wrapper try to eliminate this possibility first

<!-- gh-comment-id:2562930280 --> @ilyaLibin commented on GitHub (Dec 26, 2024): I have run the server directly with ts-node and it worked. It turned out that the cors error wasn't coming from better-auth but from serverless framework layer on top of that. fine tuning the configuration there solved the problem. If you run your app in docker or any other wrapper try to eliminate this possibility first
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8497