Skip to main content
ClerkAuth is the Auth component’s Clerk-backed strategy. It wraps the official @clerk/backend SDK to verify session tokens, resolve the current user, and manage accounts (banning, locking, metadata, sessions, and password sign-in) behind the framework’s IAuth interface. A companion ClerkAuthMiddleware plugs Clerk into the request pipeline and maps a Clerk user onto the framework IUser, so your controllers read context.user and never depend on Clerk directly.

What ClerkAuth covers

A single getCurrentUser(token) call verifies the Clerk session token with your secret key and resolves the user. ClerkAuthMiddleware does the request-side work: it extracts the bearer token, enforces route roles, and maps the Clerk user onto IUser. Beyond authentication, the strategy reaches the Clerk Backend API to ban or lock accounts, update profiles, manage metadata, and revoke sessions. Password sign-in is covered too, where signIn() verifies credentials and mints a Clerk sign-in token. Registration runs through @decorator.auth(), so there’s no manual wiring.

Installation

ClerkAuth ships with @talosjs/auth and depends on the Clerk Backend SDK.

Environment variables

The secret key is validated when ClerkAuth is constructed, so a missing key fails fast at startup rather than on the first request.

How it works

ClerkAuthMiddleware runs in the request pipeline. For each request it:
  1. Reads the token from the Authorization: Bearer <token> header (or a bearerToken query).
  2. Checks the route’s roles. If the route is guest-only (no roles or ROLE_GUEST), it lets the request through without a token.
  3. Otherwise calls ClerkAuth.getCurrentUser(token), which verifies the token and loads the Clerk user.
  4. Maps the Clerk user onto the framework IUser and sets it on context.user.
When mapping, the middleware pulls the user’s primary email, derives roles and the internal id from Clerk’s privateMetadata (falling back to ROLE_USER), and copies across name, phone, avatar, activity timestamps, and ban/lock flags.

Usage

Register the middleware (and ClerkAuth) with the container, then declare route roles. The middleware enforces them automatically.
Protect a route by declaring the roles it requires. The middleware verifies the Clerk token only when roles are present:

Password sign-in

signIn() looks up the user by email, verifies the password through Clerk, and returns the user plus a sign-in token (default TTL 30 days):

Account management

ClerkAuth exposes the Clerk user and session APIs directly:

Use in the app

In an @talosjs/app application, Clerk plugs in as a request middleware. Add ClerkAuthMiddleware to the middlewares slot of your App config. Importing it registers both ClerkAuthMiddleware and ClerkAuth with the container through @decorator.auth(), so there is nothing else to wire.
Once registered, the middleware runs on each matched route: it reads the bearer token, enforces the route’s roles, and maps the Clerk user onto context.user. Controllers then read context.user and declare the roles they require (see Protecting routes above). Set CLERK_SECRET_KEY in your .env.yml (or environment) so the strategy can verify tokens.

Exceptions

ClerkAuth and ClerkAuthMiddleware throw AuthException with a machine-readable key, so callers can branch on it.

Things worth doing

Load CLERK_SECRET_KEY from .env so it stays out of source control, never hard-coded. The middleware reads privateMetadata.roles and privateMetadata.externalId from Clerk, so set those for each user or the mapping onto IUser won’t be complete. In your handlers, read context.user rather than reaching for Clerk directly; that keeps controllers provider-agnostic and lets the auth strategy change underneath them. Declare roles on routes and let ClerkAuthMiddleware enforce them instead of re-checking tokens by hand. When something goes wrong, branch on AuthException.key and return 401 for INVALID_CREDENTIALS, MISSING_BEARER_TOKEN, and INVALID_TOKEN. See the Auth component for the strategy interface and route-protection model.