Accounts: sign-in and modules' routes
A frontend in any framework signs accounts in through SHADE's accounts, not the framework's
own auth: SHADE keeps the accounts, the passwords, the confirmation emails, the rate limits and the personal data
(export and erasure). Modules' account routes (auth: 'account') then know who is
asking.
One domain. The visitor's browser calls /api/v1/account/* on the frontend's own domain, and a proxy sends it
to SHADE's public role, so the session cookie is first-party. In production that is the path routing in front of
the frontend (on Coolify, or a Caddy as SHADE's own site has, site/Caddyfile, or a static host's forwarding:
Hosting the site on a static host); in development the
framework's own proxy:
| Framework | Development proxy for /api/v1/account/* |
|---|---|
| Next.js | rewrites: async () => [{ source: '/api/v1/account/:path*', destination: process.env.SHADE_ACCOUNT_URL + '/api/v1/account/:path*' }] in next.config.ts |
| SvelteKit | server: { proxy: { '/api/v1/account': process.env.SHADE_ACCOUNT_URL } } in vite.config.ts |
| Nuxt | routeRules: { '/api/v1/account/**': { proxy: process.env.SHADE_ACCOUNT_URL + '/api/v1/account/**' } } in nuxt.config.ts |
The frontend's address must be SHADE's Site URL (Settings). SHADE trusts that one origin with an account's
session: signing in, up and out accepts it only, the accounts' routes (/api/v1/account/session, sign-in,
sign-up, sign-out, password-reset, /api/v1/account/*) and the modules' account routes send the cookie to it
only (CORS), an account's
own data (GET /api/v1/account/data) is never read from another site, and a request that changes something must come from it (Origin). Other frontends
(Settings, Frontends) may call the public routes, never with an account's session. A request the
browser marks as coming from another site (Sec-Fetch-Site: cross-site) is refused on account routes whatever its
method, so the frontend calls them on its own domain (above) or on a subdomain of the site's.
Signing in uses the accounts' routes, from the browser (switch accounts on first, Accounts). They are SHADE's own, in the OpenAPI document with their input schemas:
| Route | Body | Answer |
|---|---|---|
GET /api/v1/account/session | { "account": { "name", "email" } }, or { "account": null } | |
POST /api/v1/account/sign-in | email, password | { "account": { "name", "email" } }, with the session cookie |
POST /api/v1/account/sign-up | name, email, password, back (the page the emails lead back to) | { "ok": true }, the same whether the address has an account or not |
POST /api/v1/account/sign-out | { "ok": true } | |
POST /api/v1/account/password-reset | email, back | { "ok": true }, the same for any address |
POST /api/v1/account/password-reset/confirm | token, password | { "ok": true } |
POST /api/v1/account/confirm | token, password | { "ok": true }, signed in |
POST /api/v1/account/delete | password | { "ok": true } |
import { callAccountRoute } from '@shadestack/client'
const post = (path: string, body: unknown) => fetch(`/api/v1/account${path}`, {
method: 'POST', credentials: 'include', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body)
})
await post('/sign-up', { name, email, password, back: `${location.origin}/account` }) // a confirmation email
await post('/sign-in', { email, password }) // once confirmed
const { account } = await (await fetch('/api/v1/account/session', { credentials: 'include' })).json()
await post('/sign-out', {})
// A module's account route: 401 (ShadeApiError) when nobody is signed in.
const wishlist = await callAccountRoute<{ sku: string }[]>('shop', '/wishlist')
await callAccountRoute('shop', '/wishlist', { method: 'POST', body: { sku: 'A-1' } })
- In Next.js, call these from a client component (
'use client'); in SvelteKit from a component or+page.tsrunning in the browser; in Nuxt from a component or composable (useAsyncData(..., { server: false })). - A server that renders for an account (a Next.js server component, a SvelteKit
load, a Nuxt server route) forwards the account'scookieheader and setsoriginto the Site URL:callAccountRoute('shop', '/wishlist', { baseUrl: SHADE_ACCOUNT_URL, headers: { cookie, origin: SITE_URL } }). Its answer is the account's own: never cache it. - That server also passes on the visitor's IP, in
X-Forwarded-For, or all its visitors share one per-IP budget: every account route is limited to 300 a minute per IP before the session is read (counted per route), as are a module's own limits perip, and without the header the IP SHADE counts is the server's. SHADE readsX-Forwarded-Forfrom the right and believes it only as far as its own network (private addresses: the proxies in front of it,apps/api/src/http/client-ip.ts), so a visitor cannot pick an IP of their own. The header therefore counts only when the server reaches SHADE from that network, such as a frontend on the same Coolify server calling SHADE's internal address; from anywhere else every request counts as the server's own address.headers: { cookie, origin: SITE_URL, 'x-forwarded-for': visitorIp }, withvisitorIpthe address the proxy in front of the frontend saw. Not simply the first address of theX-Forwarded-Forthe frontend received: the visitor can write that one, and SHADE would believe the frontend (SvelteKit'sgetClientAddress()on adapter-node withXFF_DEPTHset reads it from the right; in Next.js and Nuxt read the header from the right yourself, past your own proxies). - The routes, with their input schemas where the module describes them, are in
/api/v1/openapi.jsonunder theaccountSessionscheme, one tag per module: generate types from it with any OpenAPI tool.
Live updates, stores and kept state
Three small entries of @shadestack/client serve what a module's browser code shares with SHADE's own islands. None
of them knows a module, and none touches a browser API until it is called, so they import safely on a server.
@shadestack/client/live:listenForChanges(address, changed)follows a route's server-sent events and callschangedfor each one, and again when the stream comes back (an event only says "ask again"); it returns the function that stops it.openSocket(address, { status, message })opens a module's socket (socketAddress(base, module, path, params)builds the address), reconnects with a growing wait, and gives up when SHADE or the module has refused it.@shadestack/client/store:createStore(initial)returns{ store, set, update }. The store hassubscribe,getSnapshotandgetServerSnapshot. A new subscriber hears the snapshot at once, so Svelte's$storeworks as it is, and React reads it withuseSyncExternalStore(store.subscribe, store.getSnapshot, store.getServerSnapshot). The snapshot is replaced on every change, never changed in place. Make a store per use: one at module level would share a visitor's state with the next visitor on a server. A Vue component wraps one in ashallowRef:const state = shallowRef(store.getSnapshot()) const stop = store.subscribe(next => { state.value = next }) onScopeDispose(stop)@shadestack/client/kept: what a browser keeps between pages, the way the islands keep it. A value is JSON in local storage undershade.kept.<name>(session storage for what is personal), and a change is announced to the page with theshade:keptevent.readKept,writeKept,announceKeptandlistenKeptdo this (listenKept(heard, { tabs: true })hears other tabs too), so code that uses them shares a cart with a page built in SHADE. A browser that keeps nothing (a private window) is not an error: the value lives as long as the page. The keys and the event are a contract and do not change.
Details
- A frontend that renders the account pages with SHADE's renderer (
@shadestack/render, in Astro, Next, Nuxt or SvelteKit) gets confirmation and reset handling from the account action. A frontend with its own account page needs the same confirmation step: the token is in the link's fragment,#confirm=<token>, never in the query string (a browser never sends the fragment, so it stays out of server and proxy logs and analytics). Read it fromlocation.hash(new URLSearchParams(location.hash.slice(1)).get('confirm')), take it off the address bar at once (history.replaceState(null, '', location.pathname + location.search)), ask for the password, and post both toPOST /api/v1/account/confirm. Don't send the page's full address to analytics before the fragment is cleared. - Reset links take over the account: the link is
<the account page>#reset=<token>. Read it withnew URLSearchParams(location.hash.slice(1)).get('reset'), keep it in memory, take it off the address bar at once (history.replaceState(null, '', location.pathname + location.search)), ask for the new password, and post both in the body toPOST /api/v1/account/password-reset/confirm({ "token": "…", "password": "…" }, never the token in the URL). The account pages leave the query alone: a?token=there is the page's own (a preview's). - A refusal is SHADE's error body (
{ "error": { "code", "key", "message" } }): a wrong address or password is 401accounts.errors.passwordWrong, an unconfirmed account's sign-in 403accounts.errors.notConfirmed, a password shorter than 10 or longer than 256 characters 422accounts.errors.passwordLength, a used or expired link 422accounts.errors.linkExpired, a sign-up past the instance's accounts limit 409limits.errors.accounts, and too many attempts 429. While accounts are off, these routes answer 404 (the account parts then say "Accounts are not available on this site."). Switching accounts on without email is CONFLICT "Set up email first". - Each route is limited per IP (10 a minute, reading the session 120), and signing in, signing up and asking for a reset link per address too, however many networks ask (20 sign-ins a quarter hour, 3 emails an hour each).
- SHADE's site finds the account API through the
<meta name="shade-account-api">tag thatsite/src/layouts/Base.astrowrites (SHADE_ACCOUNT_URL, falling back toSHADE_API_URL).