Skip to content

Integrations

Microsoft Entra ID: the person from the token

The starter signs in with a development stub, and its comments name Microsoft Entra ID as the replacement for a company's own staff. Entra ID plugs in at two places: the `AuthProvider` in `src/auth/auth.tsx`, and the token source in `src/shared/http.ts`, so every api call carries a bearer token your server validates.

By Bohuslav PoláčekChecked

templates/starter/src/shared/http.ts
 * The token: the development sign-in has none, so nothing is sent until your
 * identity provider is in place. When it is, tell this file how to get a
 * token once, where the provider starts (src/auth/auth.tsx):
 *
 *   setAccessTokenSource(() => msal.acquireTokenSilent(scopes).then((r) => r.accessToken));
 *
 * The server takes the person from that token, never from the request body.
 * docs/recipes/connect-your-data.md in the framework folder has the whole contract.
 */
import type { Person } from "../workspace/core";
import { apiUrl } from "./config";

/** Gives the token for the person a call is made for, or nothing (no Authorization header). */
export type TokenSource = (person: Person) => string | null | undefined | Promise<string | null | undefined>;

let tokenSource: TokenSource = () => null;

/** Sets where tokens come from, once, when your identity provider starts. */
export function setAccessTokenSource(source: TokenSource) {
  tokenSource = source;
The starter's HTTP layer: the one call that tells every adapter where the signed-in person's token comes from.

Where it plugs in

  • src/auth/auth.tsx: the stub accepts any password for the people in users.ts. Replace its inside with your Entra ID sign-in and keep its shape: AuthProvider, useAuth, RequireAuth and a Person with a role. The rest of the app does not change.
  • src/shared/http.ts: call setAccessTokenSource() once, where the provider starts. The repo's sketch uses MSAL's acquireTokenSilent; from then on every module's HTTP adapter sends Authorization: Bearer <token>.
  • src/workspace/core.tsx: the app's roles are admin, approver and member. Your server maps what Entra ID says about the person onto one of them.
  • Your api: it validates the token on every request and takes the person from it, never from the body. Switch the modules to it with VITE_DATA_SOURCE=http (docs/recipes/connect-your-data.md).

The starter has no MSAL dependency and no Entra ID code: the seams and the sketch are what it gives you.

Steps

  1. Register the app in the client's Entra ID tenant, and expose a scope for your api.
  2. Define app roles that match admin, approver and member, and assign people or groups to them.
  3. Replace the stub in src/auth/auth.tsx with the sign-in, keeping AuthProvider, useAuth and RequireAuth.
  4. Call setAccessTokenSource() with the silent token call for your api's scope.
  5. On the server: validate the token's signature, audience and tenant, read the person's object ID and role, and refuse the rest.
  6. Delete src/auth/users.ts and the person switcher, so the stub cannot ship.

What to watch

  • Groups can be missing from the token. Entra ID leaves out the groups claim when a person is in more groups than the limit (200 in a JWT) and sends an overage claim that points to Microsoft Graph instead. App roles arrive in the roles claim, which keeps the mapping to the app's three roles short.
  • Identify a person by oid (with tid), not by e-mail. Microsoft's reference says email and preferred_username can change and must not be used for authorization.
  • Check the tenant. The tid claim says which organisation signed the person in; accept only the client's.
  • A guest is a new person. Microsoft treats a guest from another tenant as a different account; give them a role on purpose, never by default.

Sources

Questions

Does the starter include MSAL or Entra ID code?

No. It has the seams: the AuthProvider to replace and setAccessTokenSource to call. The MSAL line in the repo is a sketch in a comment, and the starter adds no dependency for it.

Should roles come from groups or from app roles?

App roles are the shorter path: they arrive in the roles claim and map one to one onto admin, approver and member. Groups work too, but a person in many groups gets an overage claim instead and the server has to ask Microsoft Graph.

Can the server identify people by their e-mail address?

Not safely. Microsoft documents e-mail and preferred_username as values that can change. Use the object ID, oid, together with the tenant ID, tid.

Does the browser decide what a person may do?

Only what the screens show. The server reads the role from the validated token and checks the same rules on every request, as the starter's comments ask.

Two ways

Build it yourself, or have it built

  • Build it yourself

    Build your app on the framework

    Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.

    One payment, lifetime updates

    Get the framework
  • Have it built

    Have us build your app

    Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.

    After launch, Care keeps it improving, from €99 a month.

    from €1,990

    Describe your app