Skip to content

Move authenticated routes to app.nemar.org (host-only cookie) #46

Closed

Description

@neuromechanist

Summary

Move authenticated researcher routes onto a dedicated app.nemar.org subdomain so the session cookie can be host-scoped (Domain=app.nemar.org) instead of wildcard-scoped (Domain=.nemar.org). Public marketing, discovery, and docs surfaces stay on nemar.org and remain fully anonymous + cacheable.

This is a tracking issue for the architectural decision; the work itself happens in lockstep with nemarOrg/nemar-cli#569 (passwordless email-code auth endpoints) so the backend can issue the cookie with the right Domain from day one. Retrofitting the cookie domain later logs every signed-in user out.

Why

The current handoff spec (.context/handoff-auth-backend.md) called for Domain=.nemar.org. That works, but the cookie then attaches to every request to:

  • api.nemar.org (intended)
  • data.nemar.org/<id>/...raw.set (byte-range fetches carry the cookie for no reason — wasted bytes per file, and every CDN tier sees the auth header)
  • docs.nemar.org (third-party docs theme, future analytics, all see authenticated-session presence)
  • Future subdomains: discuss.nemar.org, status.nemar.org, etc., each one a potential new exposure surface

A host-only cookie on app.nemar.org confines the auth surface to one host. nemar.org, data.nemar.org, docs.nemar.org stay anonymous from the browser's perspective.

Scope

Frontend (this repo)

Currently authenticated routes that move to app.nemar.org:

  • /login, /login/verify, /login/pending
  • /welcome
  • /dashboard
  • /upload
  • /dataset/[id]/collaborators
  • /admin/* (publication requests, future admin surfaces)
  • /settings

Public routes that stay on nemar.org:

  • /, /discover, /dataset/[id] (public detail page)
  • /about, /support, /community, /citation-dashboard
  • /docs/* (5 doc pages from Phase 5)
  • /signup (the CLI-deferral page — explains how to create an account; not itself an auth surface)
  • /404

One Astro codebase, one Cloudflare Pages project, two custom domains attached.

Middleware (src/middleware.ts)

Read Astro.url.hostname. Enforce the split:

  • Request hits app.nemar.org/<public-only-path> → 301 redirect to https://nemar.org/<same-path> (so app.nemar.org/discover works as a link from inside the app and lands the user on the public site without confusion)
  • Request hits nemar.org/<auth-only-path> → 301 redirect to https://app.nemar.org/<same-path> (so a stale bookmark to nemar.org/dashboard lands on the right place)
  • Localhost dev (bun run dev) bypasses the hostname check; both surfaces are reachable from http://localhost:4321

Nav

  • nemar.org Nav.astro shows public links + a single "Sign in" button → https://app.nemar.org/login. No UserMenu, no /auth/me fetch. The public site never tries to know if a user is signed in.
  • app.nemar.org Nav.astro shows the full UserMenu (account dropdown, sign out) + an "Explore datasets" link → https://nemar.org/discover.

Implementation hint: the Nav component checks the hostname (or the build-time IS_APP_SURFACE env var) and renders the correct variant.

Backend (must coordinate with nemarOrg/nemar-cli#569)

  • Set-Cookie: nemar_session=<token>; Domain=app.nemar.org; Path=/; HttpOnly; Secure; SameSite=Lax; Max-Age=<s>
  • CORS at api.nemar.org allows https://app.nemar.org (and https://*.nemar-app.pages.dev if the app surface gets its own Pages project for previews)
  • api.nemar.org does NOT allow https://nemar.org for any credentialed endpoint — the marketing site shouldn't send credentials anywhere

Cloudflare Pages config

  • One project (nemar-website)
  • Two custom domains: nemar.org (apex) and app.nemar.org
  • Both serve the same build output; middleware does the surface-split routing

Optional later: if traffic or deploy isolation calls for it, split into two Pages projects sharing the same source repo via a build-time SURFACE=public|app env var. Not needed at launch.

Implementation order

  1. Issue filed (this issue) — architectural reference, no code
  2. Backend (nemar-cli#569) ships the four OTP endpoints with Domain=app.nemar.org
  3. Frontend adds the middleware hostname-split + Nav variants (a few hours of work; mostly mechanical)
  4. Cloudflare Pages adds app.nemar.org as a custom domain; DNS for app.nemar.org points at the Pages project
  5. Cutover flips PUBLIC_AUTH_AVAILABLE=true in wrangler.toml; the maintenance card on the current /login is replaced by the actual form on app.nemar.org/login

The frontend work in (3) is mostly safe to start now even before backend ships — the middleware redirect and Nav variants don't depend on auth working. Staging on a preview deploy validates the routing.

Done criteria

  • Cookies set by api.nemar.org carry Domain=app.nemar.org only (verifiable via curl: curl -i -X POST https://api.nemar.org/auth/code/verify ... shows Set-Cookie: ... Domain=app.nemar.org)
  • nemar.org requests show no nemar_session cookie in the Cookie: request header (browser DevTools → Network → any request to data.nemar.org confirms)
  • Visiting nemar.org/dashboard 301-redirects to https://app.nemar.org/dashboard
  • Visiting app.nemar.org/discover 301-redirects to https://nemar.org/discover
  • Signing out on app.nemar.org clears the cookie and nemar.org is unchanged (because it never saw the cookie)

Out of scope (filed as separate issues when needed)

  • Discussion / forum surface. Once a discussion feature lands, the simplest path is a /discuss route on app.nemar.org. A separate discuss.nemar.org (Discourse, etc.) with SSO-bridge from app.nemar.org is the long-term option if the forum needs its own infrastructure.
  • Status page. status.nemar.org (or a hosted service like statuspage.io) is independent of this split.
  • Splitting Pages projects. If/when build-isolation between the two surfaces becomes useful, that's a follow-up; not blocking.

Related

  • Architectural reference saved in .context/handoff-auth-backend.md (and Claude project memory)
  • nemarOrg/nemar-cli#569 — backend cookie domain must be set per this issue
  • The local branch feature/auth-backend-pending (not pushed) currently has a maintenance card on /login waiting for #569 to ship

Activity

  1. added
    enhancement
    on May 22, 2026
  2. neuromechanist commented on Jul 30, 2026

    @neuromechanist
    ContributorAuthor

    Shipped. The two-host split is live in production:

    • app.nemar.org serves the authenticated routes; the session cookie is Domain=app.nemar.org (host-only), so it never rides to data.nemar.org, api.nemar.org, or docs.nemar.org.
    • src/middleware.ts 301-redirects mismatched routes across hosts; classification lives in src/lib/host.ts (isAppRoute, isProductionHost, hostMode).
    • Nav.astro renders per-surface variants.
    • Apex cutover completed 2026-07-29 (Apex cutover: ww2 becomes nemar.org, legacy moves to ww1, legacy URLs must keep resolving #190): nemar.org, www, and ww2 are all attached to the Pages project alongside app.nemar.org, one build serving four domains.

    Two deliberate departures from the spec above, both later decisions rather than gaps:

    1. Marketing routes now render on the app host for signed-in users (Signed-in users lose their session when following a marketing link #210, fixed in Keep marketing routes on the app host when signed in #211). The spec called for app.nemar.org/discover to 301 to the marketing host unconditionally. That ejected signed-in users from the only host their cookie is scoped to. getCrossHostRedirect now suppresses the marketing-bound redirect when a session cookie is present. The suppression is one-directional; marketing to app is never suppressed, because marketing responses are shared edge-cache entries and must not vary per user. Canonical origin follows the route rather than the serving host (canonicalOriginFor).
    2. ww2.nemar.org is still serving, intentionally. The old apex to ww2 bridge was a bare 301 with no Cache-Control, so browsers cached it persistently. Retiring ww2 would loop those clients forever with no server-side remedy. It stays up until those entries age out; its canonical tags already point at the apex.

    Residual gap tracked separately in #212 (staging has no app-host counterpart, so test.nemar.org runs in single-host mode and cannot exercise any of this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions