Repository navigation
Description
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 tohttps://nemar.org/<same-path>(soapp.nemar.org/discoverworks 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 tohttps://app.nemar.org/<same-path>(so a stale bookmark tonemar.org/dashboardlands on the right place) - Localhost dev (
bun run dev) bypasses the hostname check; both surfaces are reachable fromhttp://localhost:4321
Nav
nemar.orgNav.astroshows public links + a single "Sign in" button →https://app.nemar.org/login. No UserMenu, no/auth/mefetch. The public site never tries to know if a user is signed in.app.nemar.orgNav.astroshows 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.orgallowshttps://app.nemar.org(andhttps://*.nemar-app.pages.devif the app surface gets its own Pages project for previews) api.nemar.orgdoes NOT allowhttps://nemar.orgfor any credentialed endpoint — the marketing site shouldn't send credentials anywhere
Cloudflare Pages config
- One project (
nemar-website) - Two custom domains:
nemar.org(apex) andapp.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
- Issue filed (this issue) — architectural reference, no code
- Backend (
nemar-cli#569) ships the four OTP endpoints withDomain=app.nemar.org - Frontend adds the middleware hostname-split + Nav variants (a few hours of work; mostly mechanical)
- Cloudflare Pages adds
app.nemar.orgas a custom domain; DNS forapp.nemar.orgpoints at the Pages project - Cutover flips
PUBLIC_AUTH_AVAILABLE=trueinwrangler.toml; the maintenance card on the current/loginis replaced by the actual form onapp.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.orgcarryDomain=app.nemar.orgonly (verifiable via curl:curl -i -X POST https://api.nemar.org/auth/code/verify ...showsSet-Cookie: ... Domain=app.nemar.org) nemar.orgrequests show nonemar_sessioncookie in theCookie:request header (browser DevTools → Network → any request todata.nemar.orgconfirms)- Visiting
nemar.org/dashboard301-redirects tohttps://app.nemar.org/dashboard - Visiting
app.nemar.org/discover301-redirects tohttps://nemar.org/discover - Signing out on
app.nemar.orgclears the cookie andnemar.orgis 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
/discussroute onapp.nemar.org. A separatediscuss.nemar.org(Discourse, etc.) with SSO-bridge fromapp.nemar.orgis 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/loginwaiting for#569to ship
Activity
feat(auth): passwordless email-code endpoints (#569)
neuromechanist commentedon Jul 30, 2026
Shipped. The two-host split is live in production:
app.nemar.orgserves the authenticated routes; the session cookie isDomain=app.nemar.org(host-only), so it never rides todata.nemar.org,api.nemar.org, ordocs.nemar.org.src/middleware.ts301-redirects mismatched routes across hosts; classification lives insrc/lib/host.ts(isAppRoute,isProductionHost,hostMode).Nav.astrorenders per-surface variants.nemar.org,www, andww2are all attached to the Pages project alongsideapp.nemar.org, one build serving four domains.Two deliberate departures from the spec above, both later decisions rather than gaps:
app.nemar.org/discoverto 301 to the marketing host unconditionally. That ejected signed-in users from the only host their cookie is scoped to.getCrossHostRedirectnow 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).ww2.nemar.orgis still serving, intentionally. The old apex to ww2 bridge was a bare 301 with noCache-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.orgruns in single-host mode and cannot exercise any of this).