Skip to content
Nilay Kabariya

Entry No.109·Fixes··4 min read

Better Auth: Invalid origin, default secret and base URL errors

Better Auth's Invalid origin, MISSING_OR_NULL_ORIGIN, default secret and base URL errors: 24 setups on 1.7.27 and the status, code and log line each one gave.

by Nilay#node#typescript#javascriptFIXED

The error, verbatim

Jump to the fix ↓
403 {"message":"Invalid origin","code":"INVALID_ORIGIN"}

The fix that worked

Add the exact origin (scheme, host and port) to trustedOrigins:

betterAuth({ baseURL: 'http://localhost:3000', trustedOrigins: ['http://localhost:5173'] })
Not it? Every cause, tested ↓

Tested on

better-auth
1.7.27 (the current latest)
Setup
auth.handler() called with built Request objects, in-memory adapter, email and password sign-up
Node / OS
24.14.0 / Windows 11 Pro, November 22, 2026

Better Auth checks three things before it answers a request: where the request came from (the Origin header, and any callbackURL), which secret signs its cookies, and what its own base URL is. Each check has its own message, and one of them (the secret) only shows up in production.

I ran 24 setups against Better Auth 1.7.27 with the in-memory adapter, calling auth.handler() directly with a built Request for each, and recorded the HTTP status, the response body and the line Better Auth logged.

Which message means what

Response or error What I did to get it
403 {"message":"Invalid origin","code":"INVALID_ORIGIN"} a POST from an origin that is neither the baseURL nor in trustedOrigins
403 {"message":"Invalid callbackURL","code":"INVALID_CALLBACK_URL"} a callbackURL on a host that isn’t trusted
403 {"message":"Missing or null Origin","code":"MISSING_OR_NULL_ORIGIN"} sign-out with a session cookie but no Origin header
BetterAuthError: You are using the default secret. Please set BETTER_AUTH_SECRET ... no secret at all, NODE_ENV=production
BetterAuthError: Invalid base URL: localhost:3000. URL must include 'http://' or 'https://' baseURL: 'localhost:3000'
404 with an empty body a request to /auth/... when the handler expects /api/auth/...

With each INVALID_ORIGIN and INVALID_CALLBACK_URL 403, Better Auth also logged the rejected value at error level: Invalid origin: http://localhost:5173, or Invalid callbackURL: https://evil.example.net/after. That line names the exact string to compare with your config.

Invalid origin

A POST from any origin other than your baseURL is refused until that origin is in trustedOrigins. Use the exact origin the browser sends: scheme, host and port, nothing after it.

export const auth = betterAuth({
  baseURL: 'http://localhost:3000',
  trustedOrigins: ['http://localhost:5173'], // the front end's origin, as the browser sends it
});

Three near misses that still failed with Invalid origin:

  • A trailing slash. trustedOrigins: ['http://localhost:5173/'] did not match the origin http://localhost:5173. Remove the slash.
  • 127.0.0.1 against localhost. With baseURL: 'http://localhost:3000', a request from http://127.0.0.1:3000 was refused. To the check they are different origins; open the app on the same host name the config uses, or list both.
  • The production domain. With baseURL still http://localhost:3000, a request from https://app.example.com was refused. With baseURL: 'https://app.example.com' it passed without any trustedOrigins entry.

Sign-out is checked the same way: a sign-out POST from http://localhost:5173 got the same INVALID_ORIGIN 403.

Missing or null Origin

MISSING_OR_NULL_ORIGIN is the case where there is no Origin and no Referer header, or Origin: null (unless the request also sends Sec-Fetch-Site: same-origin), on a request that carries any Cookie header (on sign-in and sign-up, Sec-Fetch-* headers also trigger it with no cookie). In my runs, a sign-up POST with no Origin and no cookie passed with 200, while a sign-out POST that carried a session cookie and no Origin got the 403.

So it shows up with clients that send cookies but no Origin: a server-side fetch, a script, a native app. Send an Origin header that is in trustedOrigins from those clients. Only the plain no-header request was tested here, no native app.

Invalid callbackURL

A callbackURL is checked against the same trusted list. An absolute URL on another host needs that host’s origin in trustedOrigins:

trustedOrigins: ['https://app.example.com'], // allows callbackURL: 'https://app.example.com/after'

The secret: silent in development, an error in production

With no secret set, development ran without a single warning. The same setup with NODE_ENV=production failed on the first request with You are using the default secret. Set a long random secret in the environment that runs production:

openssl rand -base64 32
# then, in the production environment:
BETTER_AUTH_SECRET=<that value>

The error surfaced when the first request awaited the instance; betterAuth() itself did not throw. A server that stays up without a request was not tested.

A 16-character secret was accepted with two warnings: your BETTER_AUTH_SECRET should be at least 32 characters long for adequate security, and your BETTER_AUTH_SECRET appears low-entropy. The requests still worked.

The base URL

baseURL needs the scheme. With localhost:3000, the first request failed with Invalid base URL: localhost:3000. URL must include 'http://' or 'https://'; http://localhost:3000 worked. BETTER_AUTH_URL in the environment works in place of the option.

With no base URL at all, sign-up still worked, in development and in production, with a warning: Base URL is not set. Set the baseURL option or BETTER_AUTH_URL env, or use a dynamic baseURL with allowedHosts for multi-host setups. The rest of that message says the origin is then taken from each request and that callbacks and redirects may not work, which this run did not test.

What didn’t work

How this was tested

Windows 11 Pro, Node 24.14.0, better-auth 1.7.27, November 22, 2026. Each case ran in its own Node process (Better Auth reads the environment when the instance is created), with the in-memory adapter and email and password sign-up enabled. Requests were Request objects passed to auth.handler(); no browser, no framework and no network. A browser sends Origin on its own, so the header was set by hand to the value a browser would send. The secrets are placeholders. OAuth providers, Expo and other native clients, and the dynamic baseURL with allowedHosts were not tested. The script, the case list and the full output are in nk-repro/better-auth-errors.

— N.K., end of entry No.109

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.113

    Fix: TS18046 'error' is of type 'unknown' in a catch block

    Why TS18046 appeared after upgrading to TypeScript 6 or 7 with no config change, tested on 5.9.3 to 7.1 beta, and which fixes pass: instanceof, a helper, any.

    > a.ts(1,53): error TS18046: 'error' is of type 'unknown'.

    FIXED3 min
  2. No.023

    Fix: TypeScript enum is not supported in strip-only mode

    Node's ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX for enum, parameter properties, namespace, export = and import =. Two ways to run it unchanged, both tested.

    > enum Color { Red, Green }

    FIXED4 min
  3. No.021

    Fix: typescript-eslint does not support TS 7.0 (ESLint on TS 7)

    When typescript-eslint will support TypeScript 7, why npm refuses the install, and the setup that keeps TypeScript 7 while ESLint runs normally today.

    > npm error code ERESOLVE

    FIXED4 min

tip: paste the exact error text