Skip to content

Sign users in with FlowCatalyst

In this tutorial your web application stops managing passwords: people sign in on the platform’s hosted login page (password, passkey, a second factor, or their organisation’s SSO), and come back to your application signed in, with the roles they hold in your application. You will:

  1. declare a role for your application;
  2. provision a login client for it;
  3. give yourself the role;
  4. wire the login into your web application;
  5. sign in and read the user, roles and permissions.

It assumes the orders application from your first event, on a running fcdev.

sequenceDiagram
  participant B as Browser
  participant App as Your web app
  participant FC as Platform (/oauth)
  B->>App: GET /dashboard (no session)
  App-->>B: 302 to FC /oauth/authorize (PKCE)
  B->>FC: sign in on the hosted page
  FC-->>B: 302 to App callback with a code
  B->>App: GET /callback?code=...
  App->>FC: exchange code (+ verifier)
  FC-->>App: id_token (roles for this app) + access token
  App-->>B: set the app's own session cookie, 302 /dashboard

Your application keeps its own session. The id_token is read once, at the callback: it lives for 5 minutes and says who signed in and which of your application’s roles they hold. The access token that comes with it is an identity token by default: it cannot call the platform API. See tokens for why.

Roles belong to your application and are declared in code, with the permissions they grant. Add a clerk role to the definitions you sync (the platform names it orders:clerk):

const definitions = sync
.defineApplication("orders")
.withRoles([{ name: "clerk", displayName: "Clerk",
permissions: ["orders:sales:order:view", "orders:sales:order:create"] }])
.build();
await client.definitions().sync(definitions);

More on this in define and check permissions in code.

A login client is an OAuth client with the authorization_code and refresh_token grants and the openid profile email scopes, linked to your application, so the roles in its tokens are narrowed to orders.

In the console, open Applications, then Orders, and under Login Client choose Provision Login Client. Enter your application’s callback URL as the redirect URI, exactly (scheme, host and port; localhost and 127.0.0.1 are different):

Stack Redirect URI in this tutorial
TypeScript (Fastify plugin) http://localhost:3000/auth/callback
PHP (Laravel) http://localhost:8000/flowcatalyst/callback
Go and Java (application framework) http://localhost:3000/auth/callback

Pick the client type:

  • Confidential (has a secret) for a server-side web application. The TypeScript Fastify plugin needs one.
  • Public (PKCE, no secret) for an application that cannot keep a secret. Laravel and the framework support both.

Copy the client id and, for a confidential client, the secret: it is shown once. The same operation is POST /api/applications/{id}/provision-login-client with {"clientType": "CONFIDENTIAL", "redirectUris": ["..."]}. Redirect URIs, allowed origins and secret rotation are managed afterwards under OAuth clients.

Open Users, then admin@flowcatalyst.local, and in the Roles section add orders:clerk.

A user with no roles and no permissions can sign in but reaches nothing on the platform except their profile; your application would see an empty role list. The bootstrap administrator’s platform:super-admin role is not one of orders’s roles, so without this step the id_token’s roles for this client would be empty.

The Fastify plugin runs the whole flow: /auth/login, /auth/callback, /auth/logout, an encrypted session cookie, and Bearer-token checks for API callers.

Terminal window
pnpm add fastify @fastify/cookie jose
import Fastify from "fastify";
import { flowcatalystAuth, defineRbac } from "@flowcatalyst/sdk/fastify";
// Roles arrive in the id_token; permissions are resolved locally from this catalogue.
const rbac = defineRbac()
.role("orders:clerk").grants("orders:sales:order:view", "orders:sales:order:create")
.build();
const app = Fastify();
await app.register(flowcatalystAuth, {
baseUrl: "http://localhost:8080",
clientId: process.env.FC_LOGIN_CLIENT_ID!,
clientSecret: process.env.FC_LOGIN_CLIENT_SECRET!,
cookie: { secret: process.env.SESSION_SECRET!, secure: false }, // secure: false only for http://localhost
publicBaseUrl: "http://localhost:3000",
rbac,
});
app.get("/dashboard", { preHandler: app.fc.requireSession() }, async (req) => ({
id: req.principal!.id,
roles: req.principal!.roles,
canCreate: req.principal!.hasPermissionTo(["orders:sales:order:create"]),
}));
await app.listen({ port: 3000 });

Generate SESSION_SECRET once with node -e "console.log(require('@flowcatalyst/sdk/fastify').generateSessionSecret())".

Open http://localhost:3000/dashboard (http://localhost:8000/dashboard for Laravel). You are sent to the platform’s sign-in page, sign in as admin@flowcatalyst.local, and land back on the dashboard, which shows your id, the role orders:clerk and the permission check.

What happened:

  • Your application redirected to /oauth/authorize with PKCE (S256), a state and a nonce.
  • After sign-in the platform redirected back with a one-time code, which your application exchanged for tokens and verified the id_token (signature, issuer, audience, expiry, nonce).
  • The id_token’s roles held only roles of applications the login client is linked to, so another application’s roles never reach yours.
  • Your application started its own session. From here on, the platform is not involved in each request; it is asked again only to refresh or when the session ends.

The id_token carries roles. Mapping a role to permissions is your application’s own business, and each stack does it locally: defineRbac() (TypeScript), Spatie’s tables filled at sign-in or a bound RbacCatalogue (Laravel), ByRole / byRole (framework). Keep that map the same as the roles you sync.