Skip to main content
The @talosjs/auth component ships a Clerk-backed auth client and middleware. ClerkAuth verifies bearer tokens and manages Clerk users and sessions, while ClerkAuthMiddleware resolves the authenticated user onto context.user for protected routes. The package also exposes the minimal IAuth interface and @decorator.auth() decorator used by those classes.

What it handles

The middleware extracts a bearer token, verifies it through Clerk, and resolves the current user, so you don’t wire that pipeline yourself. It reads each route’s roles and only enforces authentication where it’s required; guest routes pass straight through. Controllers read context.user and never touch Clerk directly unless they need user-management or session-management operations from ClerkAuth.

How it works

Authentication runs as middleware in the request pipeline. For each request it:
  1. Extracts 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 skips verification.
  3. Otherwise verifies the token and resolves the user via getCurrentUser(token).
  4. Maps the provider’s user onto the framework IUser and sets it on context.user.
Downstream controllers read context.user and never import the auth provider directly.

Environment variables

Decorator and usage

@decorator.auth()

Registers an auth class with the container. It accepts an optional scope (defaults to singleton). A class only needs to implement getCurrentUser() to satisfy IAuth.
ClerkAuth also exposes Clerk-specific helpers for user and session management, including getUser, updateUser, signIn, signOut, getSession, banUser, and lockUser.

Protecting routes

Declare the roles a route requires in its Route decorator. The middleware enforces authentication only when roles are present. An empty list (or ROLE_GUEST) leaves the route open.
A guest route omits roles, so no token is required:

Exceptions

The component throws AuthException for authentication failures. It carries a machine-readable key, a human-readable message, and a data object, so callers can branch on the key.

Guidance

Drive access from route roles: declare roles on the route and let the middleware enforce them, rather than re-checking tokens inside controllers. Read context.user instead of the provider, which keeps controllers independent of whichever strategy is in use. When you catch an AuthException, branch on its key, returning 401 for MISSING_BEARER_TOKEN and INVALID_TOKEN; keep the keys stable and put detail in data. Load CLERK_SECRET_KEY from .env and never hard-code it. Resolve ClerkAuthMiddleware in the request pipeline and read context.user in controllers. When you need Clerk-specific operations such as signIn(), signOut(), getSession(), or user updates, inject ClerkAuth directly rather than re-implementing token verification in the controller.