[GH-ISSUE #1585] trustedOrigins ignored - Hono backend #17461

Closed
opened 2026-04-15 15:36:10 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @DiiiaZoTe on GitHub (Feb 26, 2025).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/1585

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

  1. Create a hono/bun backend and init better-auth
  2. Make sure to add the url of your nextjs app in trustedOrigins
  3. In nextjs app, use the client to sign in/signup
  4. Anything gets blocked by the trustedOrigins which should not since we added the url in trustedOrigins

Current vs. Expected behavior

Deployed hono api on port 4000, with this in auth config

trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ]

Then when I try to do anything in my nextjs app, port 4002

[0] logging trustedOrigins from auth object [ "http://localhost:4002", "http://localhost:4000" ]
[0] logging trustedOrigins from context [ "http://localhost:4000" ]
[0] Started development server: http://localhost:4000
[0] <-- POST /auth/sign-in/email
[0] 2025-02-26T21:12:00.797Z ERROR [Better Auth]: Invalid origin: http://localhost:4002
[0] 2025-02-26T21:12:00.797Z INFO [Better Auth]: If it's a valid URL, please add http://localhost:4002 to trustedOrigins in your auth config
[0]  Current list of trustedOrigins: http://localhost:4000,http://localhost:4000
[0] --> POST /auth/sign-in/email 403 7ms

The context one does not account for the 4002 port.

I even tried using the env variable BETTER_AUTH_TRUSTED_ORIGINS but same result. However, this time the context one had the 4002 port, yet it still showed the error.

What version of Better Auth are you using?

1.1.21

Provide environment information

- OS: Windows 11 and Ubuntu/Debian same issue
- Browser: Chrome

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

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  baseURL: env.API_URL,
  basePath: "/auth",
  emailAndPassword: {
    enabled: true,
  },
  advanced: {
    crossSubDomainCookies: {
      enabled: true,
    },
    trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ]
  },
  // ... anything else should be irrelevant to the issue
})

Additional context

No response

Originally created by @DiiiaZoTe on GitHub (Feb 26, 2025). Original GitHub issue: https://github.com/better-auth/better-auth/issues/1585 ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce 1. Create a hono/bun backend and init better-auth 2. Make sure to add the url of your nextjs app in trustedOrigins 3. In nextjs app, use the client to sign in/signup 4. Anything gets blocked by the trustedOrigins which should not since we added the url in trustedOrigins ### Current vs. Expected behavior Deployed hono api on port 4000, with this in auth config ```typescript trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ] ``` Then when I try to do anything in my nextjs app, port 4002 ```typescript [0] logging trustedOrigins from auth object [ "http://localhost:4002", "http://localhost:4000" ] [0] logging trustedOrigins from context [ "http://localhost:4000" ] [0] Started development server: http://localhost:4000 [0] <-- POST /auth/sign-in/email [0] 2025-02-26T21:12:00.797Z ERROR [Better Auth]: Invalid origin: http://localhost:4002 [0] 2025-02-26T21:12:00.797Z INFO [Better Auth]: If it's a valid URL, please add http://localhost:4002 to trustedOrigins in your auth config [0] Current list of trustedOrigins: http://localhost:4000,http://localhost:4000 [0] --> POST /auth/sign-in/email 403 7ms ``` The context one does not account for the 4002 port. ------------ I even tried using the env variable `BETTER_AUTH_TRUSTED_ORIGINS` but same result. However, this time the context one had the 4002 port, yet it still showed the error. ### What version of Better Auth are you using? 1.1.21 ### Provide environment information ```bash - OS: Windows 11 and Ubuntu/Debian same issue - Browser: Chrome ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ baseURL: env.API_URL, basePath: "/auth", emailAndPassword: { enabled: true, }, advanced: { crossSubDomainCookies: { enabled: true, }, trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ] }, // ... anything else should be irrelevant to the issue }) ``` ### Additional context _No response_
GiteaMirror added the lockedbug labels 2026-04-15 15:36:10 -05:00
Author
Owner

@DiiiaZoTe commented on GitHub (Feb 26, 2025):

Don't be like me...
The trustedOrigins options does not go in the advanced object.

   advanced: {
    crossSubDomainCookies: {
      enabled: true,
    },
    trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ]
  },

But instead is on the same level as advanced

   advanced: {
    crossSubDomainCookies: {
      enabled: true,
    },
  },
  trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ]

One thing I will point out though... At no point I received a type error adding trustedOrigins inside of advanced, which to me does not seem normal typescript behavior, so something is off... Maybe a biome issue?

<!-- gh-comment-id:2686394959 --> @DiiiaZoTe commented on GitHub (Feb 26, 2025): Don't be like me... The `trustedOrigins` options does not go in the `advanced` object. ```typescript advanced: { crossSubDomainCookies: { enabled: true, }, trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ] }, ``` But instead is on the same level as `advanced` ```typescript advanced: { crossSubDomainCookies: { enabled: true, }, }, trustedOrigins: [ 'http://localhost:4002', 'http://localhost:4000' ] ``` One thing I will point out though... At no point I received a type error adding `trustedOrigins` inside of `advanced`, which to me does not seem normal typescript behavior, so something is off... Maybe a biome issue?
Author
Owner

@anton89 commented on GitHub (Apr 25, 2025):

at very least this the documentation should be updated for hono integration, currently it feel like the cors setting actually being managed by hono, but it actually manage it's own rules even when you set the cors in hono to wildcard.

<!-- gh-comment-id:2830216828 --> @anton89 commented on GitHub (Apr 25, 2025): at very least this the documentation should be updated for hono integration, currently it feel like the cors setting actually being managed by hono, but it actually manage it's own rules even when you set the cors in hono to wildcard.
Author
Owner

@hahahayts commented on GitHub (Aug 9, 2025):

thanks a lot bro it is very helpful and now project is working..

<!-- gh-comment-id:3170444527 --> @hahahayts commented on GitHub (Aug 9, 2025): thanks a lot bro it is very helpful and now project is working..
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#17461