Skip to Content

Quickstart

Add sign-in to your app in about ten minutes. This walkthrough uses hosted sign-in: Foir renders the sign-in page, and your app gets the person’s tokens back. It never defines a content model. At the end you have real users who can sign in.

You’ll need a Foir project and a publishable API key. If you don’t have a project yet, create one in the console; you do not need to add any models.

Note: This is the fast path, with the SDK’s code flow on your server. To use an OpenID Connect library instead, or for the full reference (consent, apps that sign in your team, verifying tokens yourself), see Hosted sign-in. On React or Next.js, @foir/sdk/react does the wiring for you.

1. Register your app

An auth client is your app as Foir sees it. Declare it in foir.config.ts and push it. An auth client signs in one project’s users.

// foir.config.ts import { defineConfig, defineAuthClient } from '@foir/cli/configs'; export default defineConfig({ name: 'My App', authClients: [ defineAuthClient({ clientId: 'my-app', name: 'My App', redirectUris: ['https://myapp.com/auth/callback'], // A sign-in carries whatever the user's roles give them. grants: ['*'], }), ], });
foir push

foir push creates the auth client on the first push and updates it on later ones; foir push --publish releases it, and it signs nobody in until a release carries it. The redirectUris are an exact-match list: add your local URL (for example http://localhost:3000/auth/callback) while developing. See defineAuthClient for every field.

2. Send people to the sign-in page

When someone clicks “Sign in”, begin a sign-in on your server with beginSignIn from @foir/sdk/auth, keep what it returns, and redirect the browser to its URL:

import { beginSignIn, completeSignIn } from '@foir/sdk/auth'; const foir = { issuer: 'https://api.foir.dev', // or your login domain, https://login.myapp.com clientId: 'my-app', redirectUri: 'https://myapp.com/auth/callback', }; // GET /sign-in const pending = await beginSignIn(foir); session.set('foirSignIn', { state: pending.state, codeVerifier: pending.codeVerifier, nonce: pending.nonce }); return redirect(pending.url);

Keep the state, verifier and nonce in your server’s session, or in an HttpOnly cookie: never in a URL or in browser JavaScript. Foir’s page handles the password, the emailed code, passkeys and providers from here.

3. Handle the callback

Foir sends the person back to your redirectUri with code and state. completeSignIn checks them against what you kept, redeems the code and returns the tokens:

// GET /auth/callback const tokens = await completeSignIn(foir, request.url, session.get('foirSignIn')); session.unset('foirSignIn'); // tokens.accessToken, tokens.refreshToken, tokens.claims.sub (the user id), tokens.claims.email

You now have a signed-in user. Keep the tokens however your app manages sessions, or swap them for an HttpOnly cookie with a first-party session.

4. Call the API as the user

Send the access token as a bearer token alongside your API key:

curl https://api.foir.dev/graphql \ -H "Authorization: Bearer <token>" \ -H "x-api-key: <your-public-key>"

The 15-minute access token is an EdDSA JWT. To verify it on your backend, fetch the public keys from https://api.foir.dev/.well-known/jwks.json and check that its issuer is the issuer the discovery document names: https://api.foir.dev, or your login domain for someone who signed in through it. Never ship a verifier to the browser. When it expires, refresh it at the token endpoint or with refreshToken. See User Authentication for the token model.

What’s next

  • How auth is billed: you start counting monthly active users as people sign in.
  • Webhooks: define an operation and attach it as a hook on USER_CREATED to have the event reach your backend.
  • Custom login domain: serve the sign-in page from login.your-domain.com, which then becomes the issuer of the people who sign in through it. See Config reference.
  • No backend? A verified login domain also lets a single-page app hold an HttpOnly session with no tokens in JavaScript. See SPA (no backend).
  • Build your own sign-in UI instead? The Token API exposes signIn, register, and OTP directly.
  • On React or Next.js? React & Next.js gives you a provider, hooks and drop-in components.
Last updated on