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 singlegetCurrentUser(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
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:
- Reads the token from the
Authorization: Bearer <token>header (or abearerTokenquery). - Checks the route’s
roles. If the route is guest-only (no roles orROLE_GUEST), it lets the request through without a token. - Otherwise calls
ClerkAuth.getCurrentUser(token), which verifies the token and loads the Clerk user. - Maps the Clerk user onto the framework
IUserand sets it oncontext.user.
privateMetadata (falling back to ROLE_USER), and copies across name, phone, avatar, activity timestamps, and ban/lock flags.
Usage
Register the middleware (andClerkAuth) with the container, then declare route roles. The middleware enforces them automatically.
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.
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
LoadCLERK_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.