First-Party Sessions
Hosted sign-in hands your app short-lived access tokens. That suits a mobile app, but for a web app you often want the user’s session to be a plain first-party session: a single HttpOnly cookie on your own domain, with no token touched by browser JavaScript at all.
There are two ways to get one. Start with the first: it needs no server of your own.
Foir-hosted sessions (no backend)
Foir can own the session for you. You get a long-lived HttpOnly cookie on your domain and a short-lived token in memory, and Foir sets the cookie, so there is nothing of yours to run.
- Register an auth client with a custom login domain on the same registrable domain as your app (
app.acme.com+login.acme.com), and verify it. - On login, Foir sets
foir_session(HttpOnly; Secure; SameSite=Lax; Domain=.acme.com), so the cookie reaches your app. - Your SPA calls
GET https://login.acme.com/session/tokenwithcredentials: 'include'. Foir validates the cookie and returns a 15-minute access token for the app the login domain fronts. Another app of the same project on that site names itself with?client_id=(the SDK’sclientId), and the call is answered only from an origin that app lists. - The SPA uses that bearer against
api.foir.dev, and silently re-exchanges the cookie when it nears expiry.
The cookie never touches the data plane, and the token never touches storage, so there is nothing for cross-site scripting to exfiltrate.
@foir/sdk does all of this for you; see the SPA quickstart. Note there is no refresh token in this flow: re-exchanging the cookie is the refresh, so there is nothing to rotate and nothing to store. An app with a server that renders per request can make the same exchange there instead of in the browser: see SSR (hosted session).
The custom login domain is required. A cookie can only be sent on a background request if it is first-party, so an SPA on a domain Foir does not share cannot hold a Foir session: the session endpoint refuses credentialed cross-site calls by design. If you can’t add a DNS record, use the hosted sign-in code flow and keep the access token in memory.
Your own session, on the server that renders your pages
If your web app already has a server that renders pages (Next.js, Remix, Hydrogen/Oxygen, Rails, a Worker), that server can own the session instead. It runs the code flow itself, keeps the user’s tokens, and sets its own session cookie. Foir is the identity provider and the API; your app owns the session.
Be exact about what that server is:
- It is the user acting as themselves. It reads and renders with its server key (A server rendering your pages) and the user’s own bearer, so every request is gated by that user’s role, exactly as if their browser had made it.
- It never holds an admin key, and it never calls Foir for a user whose bearer it does not hold. Nobody signs in through an admin key, and a user’s bearer sent beside one is refused.
- It is not a proxy for your app’s logic. Anything your app does for the user beyond what their role lets them do themselves is an operation, which the user runs and whose endpoint writes with the dispatch token it is called with.
When to use it
- Your web app has a server that renders per request and can set a cookie.
- You want the browser to never see an access or refresh token (defends against token exfiltration via XSS).
- You want a normal
HttpOnly; Secure; SameSitesession cookie and server-side logout/revocation.
How it works
Browser (your-domain.com) Your server (your-domain.com) Foir
│ │ │
│ GET /login │ │
│─────────────────────────────────►│ beginSignIn: discovery │
│ │─────────────────────────────►│
│ 302 → Foir's sign-in page ◄─────┼──────────────────────────────│
│ …user authenticates (pwd/OTP/SSO) at Foir… │
│ 302 → /callback?code=&state= ───►│ completeSignIn: /oauth/token│
│ │─────────────────────────────►│
│ │ ◄ { accessToken, refreshToken, idToken }
│ Set-Cookie: session=… │ (store tokens server-side, │
│ HttpOnly; Secure; SameSite=Lax ◄│ keyed by the session id) │
│ │ │
│ GET /account (cookie only) │ server key + Bearer <token> │
│─────────────────────────────────►│─────────────────────────────►│ api.foir.devThe browser only ever holds an opaque, HttpOnly session cookie. Your server keeps the user’s access and refresh tokens, refreshes them transparently, and sends the bearer when it renders a page for that user.
Build it
-
Register an auth client for your app (Register your app). Your server is what runs the code flow.
-
Start the sign-in. On
GET /login, callbeginSignInfrom@foir/sdk/auth(or your OpenID Connect library), keep the state, verifier and nonce it returns in your session store, and 302 the browser to its URL. The verifier never goes to the browser. -
Handle the callback. On your registered redirect URI, call
completeSignInwith the URL the browser came back to and what you kept, then delete what you kept. You get back{ accessToken, refreshToken, idToken, claims }, whereclaims.subis the user id. -
Create your session. Generate an opaque session id, store
{ userId, accessToken, refreshToken, expiresAt }server-side keyed by it, and set the cookie:Set-Cookie: session=<opaque-id>; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=…Because the cookie is set by your own origin, it is first-party to your app: no third-party-cookie restrictions, no token in
localStorage. -
Render as the user. On each request, read the session cookie, look up the stored tokens, and call the Foir public API with your rendering server’s key,
Authorization: Bearer <token>, and the visitor’s address inX-Foir-Visitor-IP(the SDK’svisitoroption). When the 15-minute access token is near expiry, rotate the stored refresh token at the token endpoint (refresh tokens are single-use and rotate on every call, so store the new one). -
Logout. Delete the server-side session, clear the cookie, and optionally revoke the refresh token at
/oauth/revoke, naming your auth client withclient_id(or HTTP Basic, for a confidential client).
Why hold it on your server
- No tokens in the browser. Access and refresh tokens never leave your server, so XSS can’t exfiltrate them. The cookie is
HttpOnly, so script can’t read it either. - Server-side refresh and revocation. Rotation and reuse-detection happen on your server; you can kill a session instantly.
- Standard session semantics. Your app behaves like any normal first-party web app, and the framework’s existing session and CSRF tooling applies.
Custom login domains (white-label)
With a custom login domain the sign-in happens at login.your-domain.com, so your users never leave your brand, and the domain is the issuer of their tokens. That removes the off-brand redirect that trains people to trust look-alike phishing pages.
For a Foir-hosted session it is load-bearing: it is what makes Foir’s session cookie first-party to your app. For a session your own server holds, it is purely a presentation upgrade, since your server owns the session either way.