openship icon indicating copy to clipboard operation
openship copied to clipboard

Cannot login with email and password

Open inoguerols opened this issue 4 months ago • 4 comments

inoguerols avatar Jul 19 '26 17:07 inoguerols

image

inoguerols avatar Jul 19 '26 17:07 inoguerols

Additional diagnostic data from a fresh self-hosted instance.

TL;DR — sign-in/email returns 200, but the very next get-session returns 401. The credentials are accepted; the session cookie never comes back.

Environment

  • Openship CLI 0.1.11, installed via npm i -g openship
  • Instance started with openship up (launchd agent, macOS)
  • macOS 26.5.1, arm64 (Apple Silicon)
  • Node.js 26.0.0
  • Dashboard http://localhost:3001, API http://localhost:4000
  • Auth mode: local (instance had been promoted from zero-auth)

Steps to reproduce

  1. openship up on a clean instance (no prior ~/.openship/data)
  2. Create the account through the dashboard — succeeds
  3. Log in at /login with that email and password
  4. Login appears to fail; UI stays unauthenticated / redirects back

Server logs during a login attempt

<-- POST /api/auth/sign-in/email
--> POST /api/auth/sign-in/email 200 125ms
<-- GET /api/auth/get-session
--> GET /api/auth/get-session 401 1ms
--> GET /api/projects/home 401 1ms
<-- GET /api/auth/get-session
--> GET /api/auth/get-session 401 0ms

sign-in/email succeeding with 200 while every subsequent get-session returns 401 means authentication itself works — password hashing and lookup are fine. What fails is session persistence.

Analysis

The dashboard (:3001) and the API (:4000) are different origins. This looks like the session cookie is either not being set with attributes the browser will accept across that port boundary, or not being sent back on subsequent requests. Common causes in this shape of setup:

  • SameSite/Secure attributes incompatible with plain-HTTP localhost across two ports
  • Cookie Domain mismatch between localhost and 127.0.0.1
  • Dashboard calling the API on a different host string than the one that issued the cookie

Side effects

This also blocks the CLI indirectly: a Personal Access Token can only be created from the dashboard, so an instance in local auth mode where the dashboard cannot hold a session becomes unusable from both surfaces. Recovering required stopping the service, moving ~/.openship/data and ~/.openship/auth-secret aside, and restarting to get back to zero-auth.

Happy to test a patch or provide more logs.

inoguerols avatar Jul 19 '26 17:07 inoguerols

Dug into the source to narrow this down. Three likely causes ruled out with evidence — posting so nobody re-treads this ground.

❌ Not CORS

Live probe against a running desktop instance (deliberately wrong password, so the response headers are still representative):

$ curl -D- -X POST -H 'Origin: http://localhost:3001' \
    -H 'Content-Type: application/json' \
    -d '{"email":"[email protected]","password":"..."}' \
    http://localhost:4000/api/auth/sign-in/email

HTTP/1.1 401 Unauthorized
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:3001
vary: Origin

Explicit origin (not *) plus allow-credentials: true — correct for a credentialed cross-origin request.

❌ Not a missing credentials: "include"

apps/dashboard/src/lib/auth-client.ts sets it on the Better Auth client:

export const authClient = createAuthClient({
  baseURL: AUTH_BASE_URL,
  fetchOptions: {
    credentials: "include",
    ...

❌ Not a host mismatch between dashboard and API

packages/core/src/runtime-config.ts builds both URLs from the same localhost helper (dashboard: 3001, api: 4000), so the cookie host matches and cookies ignore ports.

❌ Not the org shield

apps/api/src/middleware/better-auth-shield.ts only gates /api/auth/organization/*. It never touches sign-in/email or get-session.

Where that leaves it

In desktop (non-cloud) mode, getSharedCookieDomain() in apps/api/src/lib/auth.ts returns undefined, so advanced carries only cookiePrefix — no crossSubDomainCookies, no explicit defaultCookieAttributes. Session cookie attributes therefore fall through to Better Auth defaults.

Worth checking next:

  1. What Set-Cookie attributes actually come back on a successful sign-in/email in desktop mode — specifically whether SameSite=None is emitted without Secure, which browsers silently drop over plain HTTP.
  2. Whether trustedOrigins includes the dashboard origin in desktop mode.
  3. Whether the cookie is set on the API origin but the session lookup expects a different cookiePrefix.

Item 1 is my main suspect: it would explain a 200 on sign-in with an immediate 401 on get-session, and it would be invisible server-side because the server does everything right — the browser is the one discarding the cookie.

inoguerols avatar Jul 19 '26 17:07 inoguerols

Found it. It's not the cookie attributes after all — my earlier suspicion (item 1) was wrong. The dashboard and the API are on different sites, not just different ports.

Root cause

The CLI advertises the dashboard on localhost, but hands the browser a 127.0.0.1 API origin.

apps/cli/src/commands/up.ts prints and opens http://localhost:3001 (up.ts:84, up.ts:245), while the dashboard child process is spawned with:

OPENSHIP_LOCAL_API_URL: `http://127.0.0.1:${port}`,   // up.ts:219

That value reaches the browser as window.__OPENSHIP_API_ORIGIN__ (apps/dashboard/src/app/layout.tsx:96) → localApiOverride() (apps/dashboard/src/lib/api/urls.ts:83-91) → getAuthBaseUrl() (urls.ts:117-123) → the Better Auth client's baseURL (auth-client.ts:11).

Verified live against a running instance:

$ curl -s http://localhost:3001/login | grep -o '__OPENSHIP_API_ORIGIN__[^<]*'
__OPENSHIP_API_ORIGIN__="http://127.0.0.1:4000"

localhost and 127.0.0.1 resolve to the same machine but are different sites to a browser. So the sign-in fetch from localhost:3001 to 127.0.0.1:4000 is cross-site, and the cookie that comes back is:

openship.session_token=...; Path=/; HttpOnly; SameSite=Lax

Host-only, SameSite=Lax, no Secure — the browser stores it under 127.0.0.1, never sends it back on the get-session call, and the desktop override (apps/api/src/modules/auth/auth.controller.ts:84-102, registered because DEPLOY_MODE=desktop) then correctly returns 401 for a request with no session.

This is why the server logs look innocent: the server does everything right; the browser is discarding the cookie. It also explains why my earlier curl probes couldn't reproduce it — curl ignores SameSite and the host distinction.

Fix

PR incoming. I put the fix in localApiOverride() rather than in the CLI: normalising the injected origin against window.location.hostname (both loopback → adopt the page's host, port preserved) fixes it in the shared place every caller routes through, and covers the symmetric case too — opening the dashboard on 127.0.0.1:3001 would otherwise break the same way with a localhost API origin, and Electron injects the same variable.

Cookie config is untouched — nothing needed relaxing, SameSite=Lax is correct here once the two surfaces are same-site.

inoguerols avatar Jul 19 '26 18:07 inoguerols