Skip to content

fix(auth): keep SSO working when the app and API are on different ori… - #1202

Open
njbrake wants to merge 1 commit into
thunderbird:mainfrom
njbrake:pr3-split-origin-auth
Open

fix(auth): keep SSO working when the app and API are on different ori…#1202
njbrake wants to merge 1 commit into
thunderbird:mainfrom
njbrake:pr3-split-origin-auth

Conversation

@njbrake

@njbrake njbrake commented Aug 6, 2026

Copy link
Copy Markdown

…gins

Note: this PR description was drafted by Claude via back-and-forth with @njbrake. The reasoning and decisions are his; the prose is Claude's.

Fixes two SSO failures that only appear when the app and the API are served from different origins. Both are invisible in the Compose and ALB setups, where one origin proxies both.

Sign-in returns a bare NOT_FOUND. Better Auth resolves its error redirect as onAPIError.errorURL || ${baseURL}/error, and baseURL is the API origin, which serves no HTML. Any auth failure lands on a blank 404 instead of the app. Now sets onAPIError.errorURL to APP_URL.

The OAuth state cookie is rejected. __Secure-better-auth.state is set by the API origin and read back during a redirect initiated from the app origin, so a cross-site SameSite=Lax cookie is dropped and sign-in fails. account.skipStateCookieCheck is now enabled only when APP_URL and the backend origin actually differ, so single-origin deployments keep the check.

PKCE still protects the exchange, and the state parameter is still round-tripped through the provider. The cookie is a second layer that a split-origin deployment cannot rely on.

…gins

_Note: this PR description was drafted by Claude via back-and-forth with @njbrake. The reasoning and decisions are his; the prose is Claude's._

Fixes two SSO failures that only appear when the app and the API are served from different origins. Both are invisible in the Compose and ALB setups, where one origin proxies both.

**Sign-in returns a bare `NOT_FOUND`.** Better Auth resolves its error redirect as `onAPIError.errorURL || ${baseURL}/error`, and `baseURL` is the API origin, which serves no HTML. Any auth failure lands on a blank 404 instead of the app. Now sets `onAPIError.errorURL` to `APP_URL`.

**The OAuth state cookie is rejected.** `__Secure-better-auth.state` is set by the API origin and read back during a redirect initiated from the app origin, so a cross-site `SameSite=Lax` cookie is dropped and sign-in fails. `account.skipStateCookieCheck` is now enabled only when `APP_URL` and the backend origin actually differ, so single-origin deployments keep the check.

PKCE still protects the exchange, and the state parameter is still round-tripped through the provider. The cookie is a second layer that a split-origin deployment cannot rely on.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant