# Skill: Auth Boundary Use this when a website needs signed-in user state, login, logout, account-gated pages, profile loading, or protected file downloads. This template does not implement its own identity provider. The platform owns auth and exposes a small contract to the website. ## Contract - Browser code must call app-local endpoints only. - Server code may call `https://fnf.internal/*`. - Do not send auth headers to `fnf.internal`; identity is attached by the platform/runtime automatically. - Do not call `https://fnf.internal/*` from the browser. It is not reachable there and would be a security bug even if it were. Endpoints: | Purpose | Endpoint | Caller | |---|---|---| | Current user profile | `GET https://fnf.internal/user` | server only | | Browser-safe user proxy | `GET /api/user` | frontend/browser | | Start login | `GET /__auth/login?return=` | browser navigation | | Start logout | `GET /__auth/logout?return=` | browser navigation | `GET https://fnf.internal/user` returns the current user's profile JSON and returns `401` when the visitor is not signed in. `GET /api/user` must call `https://fnf.internal/user` server-side and return the same status code and JSON body unchanged. This endpoint is the only user-profile endpoint browser components should fetch. ## Choose The Correct Auth Mode There are two different auth concepts. Pick intentionally: | Website need | Auth mode | Required behavior | |---|---|---| | Higgsfield SDK/model generation, media upload, profile, workspace, credits, feed/history | Higgsfield platform auth | Use `/api/user`, `/__auth/login`, `/__auth/logout`, and server-side `https://fnf.internal` guards | | The generated website's own product accounts, for example todos, notes, CRM records, dashboards, memberships | In-app auth | Build app-local auth/session/storage. Do not call `fnf.internal/user` unless the website also uses Higgsfield SDK features | | Both generation and product accounts | Both, kept separate | Gate SDK routes with Higgsfield auth and product data with in-app auth. Label UI clearly and never mix identities | If the prompt mentions Higgsfield, SDK, model generation, Nano Banana, Seedance, image/video generation, media upload, credits, workspaces, or generation history, Higgsfield auth is implicit even if the user did not say "add sign in." If the prompt only asks for "sign in" for a normal generated product and does not use Higgsfield generation/profile/credits, default to in-app sign in. Do not use `/__auth/login` as the product's account system unless the prompt specifically asks for Higgsfield account sign-in. ## Required `/api/user` Route Create a TanStack Start server route, not a separate Hono/Express API: ```ts // app/src/routes/api/user.ts import { createFileRoute } from '@tanstack/react-router' export const Route = createFileRoute('/api/user')({ server: { handlers: { GET: async () => { const upstream = await fetch('https://fnf.internal/user') const body = await upstream.text() return new Response(body, { status: upstream.status, headers: { 'content-type': upstream.headers.get('content-type') ?? 'application/json', 'cache-control': 'no-store', }, }) }, }, }, }) ``` Notes: - Preserve upstream status. A `401` from `fnf.internal/user` must remain `401` at `/api/user`. - Preserve upstream JSON body unchanged. Do not reshape fields in the proxy. - Add `cache-control: no-store`; user identity is per request. - Do not add `Authorization`, cookies, service secrets, `hf-user-id`, or marketplace headers. The platform injects identity on server egress. - Do not expose raw fnf internal URLs to the UI. ## Frontend User Loader Pattern Use `/api/user` from loaders, queries, or components: ```ts export async function fetchCurrentUser() { const response = await fetch('/api/user', { credentials: 'include' }) if (response.status === 401) return null if (!response.ok) throw new Error('Failed to load user') return response.json() } ``` For route loaders, return `null` for signed-out users rather than crashing the page. For account-required pages, redirect or render a sign-in action. ## Login Login is browser navigation, not an SDK call: ```ts function login(returnPath = window.location.pathname + window.location.search) { window.location.href = `/__auth/login?return=${encodeURIComponent(returnPath)}` } ``` Rules: - Use the current path as `return` by default. - Encode the return path. - Keep the return path app-local. Do not pass full external URLs. - After login completes, the platform redirects back to `return`; then `/api/user` should succeed. ## Logout Logout is also browser navigation: ```ts function logout(returnPath = '/') { window.location.href = `/__auth/logout?return=${encodeURIComponent(returnPath)}` } ``` Rules: - Use `/` or the current public route as the default `return`. - Clear client-side query/cache state after navigation if the website stores user, workspace, feed, or credits data in TanStack Query. - Do not try to delete platform cookies manually from website code. - Do not call `fnf.internal` for logout. ## Authenticated File Downloads Apps may run inside a cross-origin authenticated iframe. Native download navigation may not preserve that embedded authentication context and can return `401 Unauthorized`. For a protected app-local `/api/...` file, NEVER use: - `` - `window.location` - `window.open` Fetch the file with credentials and download the successful response through a temporary Blob URL instead. This is a browser-only helper: call it from an event handler, never during SSR or module initialization. ```ts export async function downloadAuthenticatedFile( url: string, filename: string, ): Promise { const downloadUrl = new URL(url, window.location.href) if ( downloadUrl.origin !== window.location.origin || !downloadUrl.pathname.startsWith('/api/') ) { throw new Error('Authenticated downloads require an app-local /api/... URL') } const response = await fetch(downloadUrl, { credentials: 'include', }) if (!response.ok) { throw new Error(`Download failed (${response.status})`) } const blobUrl = URL.createObjectURL(await response.blob()) const anchor = document.createElement('a') try { anchor.href = blobUrl anchor.download = filename anchor.style.display = 'none' document.body.appendChild(anchor) anchor.click() } finally { anchor.remove() window.setTimeout(() => URL.revokeObjectURL(blobUrl), 0) } } ``` Call the helper from a real Quanta button. The handler owns loading state and must catch and surface download errors through designed inline feedback or a toast. ```tsx ``` Requirements: - The protected server route must re-check the current user and authorize that specific file. Return `401`/`403` for unauthenticated/forbidden requests; never redirect a download fetch to an HTML login page. Serve the file with a truthful `Content-Type` and, when the route owns the filename, matching `Content-Disposition`. - Pass `credentials: 'include'`. The helper enforces a same-origin `/api/...` URL; never weaken that check or pass it an untrusted arbitrary URL. - Check `response.ok` before reading the body. - Show loading and error states. - Remove every temporary element and revoke every object URL after the click has had a browser task to start the download. - Do not nest a ` ) } ``` Do not render active prompt fields, upload controls, generate buttons, cost buttons, workspace switchers, or job feeds as usable controls while signed out. ## Anti-Patterns ```ts await fetch('https://fnf.internal/user') // bad in browser code await fetch('/api/user') // good in browser code ``` ```ts fetch('https://fnf.internal/user', { headers: { Authorization: `Bearer ${token}` }, }) // bad; platform attaches identity automatically ``` ```ts window.location.href = '/login' // bad; not the platform auth route window.location.href = '/__auth/login?return=%2Fdashboard' // good ``` ```ts // bad: anonymous generation surface await jobs.submit(input) // good: server-side auth gate first const auth = await requireCurrentUser() if (!auth.ok) { return new Response(JSON.stringify(auth.body), { status: auth.status, headers: { 'content-type': 'application/json' }, }) } await jobs.submit(input) ```