SHADE is young: 0.1.0-next.2 is a pre-release, and 0.1.0 isn't out yet.

SHADE
Sites in other frameworks

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:

FrameworkDevelopment proxy for /api/v1/account/*
Next.jsrewrites: async () => [{ source: '/api/v1/account/:path*', destination: process.env.SHADE_ACCOUNT_URL + '/api/v1/account/:path*' }] in next.config.ts
SvelteKitserver: { proxy: { '/api/v1/account': process.env.SHADE_ACCOUNT_URL } } in vite.config.ts
NuxtrouteRules: { '/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:

RouteBodyAnswer
GET /api/v1/account/session{ "account": { "name", "email" } }, or { "account": null }
POST /api/v1/account/sign-inemail, password{ "account": { "name", "email" } }, with the session cookie
POST /api/v1/account/sign-upname, 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-resetemail, back{ "ok": true }, the same for any address
POST /api/v1/account/password-reset/confirmtoken, password{ "ok": true }
POST /api/v1/account/confirmtoken, password{ "ok": true }, signed in
POST /api/v1/account/deletepassword{ "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.ts running 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's cookie header and sets origin to 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 per ip, and without the header the IP SHADE counts is the server's. SHADE reads X-Forwarded-For from 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 }, with visitorIp the address the proxy in front of the frontend saw. Not simply the first address of the X-Forwarded-For the frontend received: the visitor can write that one, and SHADE would believe the frontend (SvelteKit's getClientAddress() on adapter-node with XFF_DEPTH set 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.json under the accountSession scheme, 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 calls changed for 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 has subscribe, getSnapshot and getServerSnapshot. A new subscriber hears the snapshot at once, so Svelte's $store works as it is, and React reads it with useSyncExternalStore(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 a shallowRef:
    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 under shade.kept.<name> (session storage for what is personal), and a change is announced to the page with the shade:kept event. readKept, writeKept, announceKept and listenKept do 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 from location.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 to POST /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 with new 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 to POST /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 401 accounts.errors.passwordWrong, an unconfirmed account's sign-in 403 accounts.errors.notConfirmed, a password shorter than 10 or longer than 256 characters 422 accounts.errors.passwordLength, a used or expired link 422 accounts.errors.linkExpired, a sign-up past the instance's accounts limit 409 limits.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 that site/src/layouts/Base.astro writes (SHADE_ACCOUNT_URL, falling back to SHADE_API_URL).