Skip to Content
Auth for your appSSR (hosted session)

SSR (hosted session)

Add sign-in to a server-rendered app — Remix, Hydrogen, Next.js in server mode, anything whose loader runs per request — using a Foir-hosted session. Foir renders the login UI on your own domain and sets a long-lived HttpOnly cookie there; your server exchanges that cookie for a short-lived user bearer on each request.

This is the SPA flow with the exchange moved from the browser to your server. It suits an app that has a server but does not want to own a session store, and it is the shape a storefront usually wants: the loader already runs per request, so exchanging there costs nothing extra and the bearer never reaches the browser at all.

If your server does want to own the session, use the session on your own server instead: you keep the user’s tokens and set your own cookie.

Note: As with the SPA flow, this needs a custom login domain on the same registrable domain as your app — app.acme.com plus login.acme.com. That is what makes the cookie first-party. Register it on the auth client exactly as SPA (no backend) describes.

How it works

Browser (app.acme.com) Your server Foir (login.acme.com) │ │ │ │ GET / │ │ │ Cookie: foir_session │ │ │────────────────────────────────►│ GET /session/token │ │ │ (cookie forwarded) │ │ │─────────────────────────────►│ │ │ ◄── { access_token, │ │ │ expires_in: 900 } │ │ │ │ │ │ POST /graphql Bearer … │ │ │────────────────────────────► api.foir.dev │ ◄── rendered HTML │ │

There is no refresh token in this flow. The cookie is the durable credential and the bearer is re-exchanged when it dies, so there is nothing to rotate and nothing to store.

Reading the session

fetchHostedSession takes the incoming Request and returns an outcome:

import { fetchHostedSession } from '@foir/sdk/auth'; const config = { loginOrigin: 'https://login.acme.com' }; export async function loader({ request }) { const result = await fetchHostedSession(request, config); switch (result.outcome) { case 'active': // result.session: { token, userId, email, expiresIn } return { user: { id: result.session.userId, email: result.session.email } }; case 'signed-out': // 'no-cookie' — nothing to exchange. 'rejected' — the front door refused // it and has already cleared the cookie. return { user: null }; case 'unavailable': // Says NOTHING about whether the user is signed in. Keep the session // you already have, serve the request, try again on the next one. return { user: null, degraded: true }; } }

Do not collapse the three outcomes to null. unavailable covers a 502, a timeout and a stripped CSRF header; treating any of those as “signed out” logs a user out whose 30-day cookie is perfectly valid. That distinction is the whole reason the function reports an outcome instead of returning a session or nothing.

expiresIn is seconds from when the exchange answered, so store the death date rather than the number. Zero means the endpoint reported no lifetime — treat it as “expires imminently” and re-exchange, never as “never expires”.

Signing in

Redirect to the front door. hostedSignInUrl builds the URL and forwards where to come back to:

import { hostedSignInUrl } from '@foir/sdk/auth'; export async function action({ request }) { const returnTo = new URL(request.url).searchParams.get('returnTo') ?? '/'; return redirect(hostedSignInUrl(config, new URL(returnTo, request.url).toString())); }

When one auth client serves an address per organization, name the organization the person is signing in to. It decides the sign-in’s branding and pool, and which of that organization’s addresses they may come back to:

return redirect(hostedSignInUrl(config, returnTo, { org: organization.id }));

The front door passes it to the sign-in. An organization the login domain or the app is bound to differently, one of another project, or one that is not active is refused rather than ignored.

Your server sends no browser origin when it exchanges the cookie, so this flow needs nothing in allowedOrigins for a host per organization. A new organization’s host on the login domain’s registrable domain (north.acme.com beside login.acme.com) signs in as soon as the organization exists, and its app base URL is what lets its people come back to it.

Signing out

Sign-out is a redirect too, and it is the only thing that ends the session. The cookie is the durable credential: discarding a bearer, clearing your own session, or calling the signOut mutation all leave it in place, and the next request re-establishes the user from it.

import { hostedSignOutUrl } from '@foir/sdk/auth'; export async function action() { return redirect(hostedSignOutUrl(config, 'acme-storefront', 'https://app.acme.com/')); }

That points at the front door’s /session/end-session endpoint, which revokes the session row, clears the cookie, and returns the browser to your URL. It is OIDC RP-Initiated Logout  in shape, so an off-the-shelf OIDC client gets it for free — the endpoint is published as end_session_endpoint in your login domain’s OIDC discovery document.

Being a plain redirect is what makes it reachable from a server: no CORS, no CSRF header, no cookie relaying, and it works with JavaScript off.

  • The post-logout URL must be absolute and on an origin registered for that client_id. Anything else is refused. That is why client_id is required whenever you pass one.
  • Omit the return URL for a bare 204 with no redirect.
  • It is idempotent: a missing or already-revoked session still clears the cookie and still redirects.

Config

Every path is configuration rather than a constant, so a domain restructure is a config change:

const config = { loginOrigin: 'https://login.acme.com', // required cookieName: 'foir_session', // default sessionPath: '/session/token', // default endSessionPath: '/session/end-session', // default clientId: 'acme-shop', // default: the login domain's own app };

What’s next

Last updated on