Server Authentication
When building API routes or server actions in applications authenticated by Wacht, you must independently verify the incoming request’s session JWT to ensure the user is who they claim to be. The@wacht/backend SDK provides highly optimized verify-only primitives: getAuth() and requireAuth().
These functions leverage the jose library to cryptographically verify the JWT locally using your Public Signing Key, completely avoiding network round-trips to the Wacht API for every request.
getAuth()
getAuth() evaluates the Authorization: Bearer <token> header of a standard web Request object. It returns a WachtAuth object containing the session’s claims and utility methods, regardless of whether the user is successfully authenticated.
This makes it ideal for mixed-access routes where signed-out users see public content while signed-in users see personalized content.
requireAuth()
requireAuth() works exactly like getAuth(), but it strictly enforces authentication. If the request does not contain a valid, unexpired session token, it immediately throws a WachtAuthError (HTTP 401).
Use this for fully protected routes that strictly require an active user session.
The WachtAuth Object
Both getAuth() and requireAuth() resolve to a WachtAuth object representing the current authenticated context.
Properties
| Property | Type | Description |
|---|---|---|
userId | string | null | The unique identifier of the authenticated user. |
sessionId | string | null | The specific JWT session identifier. |
organizationId | string | null | The ID of the currently active Organization. |
workspaceId | string | null | The ID of the currently active Workspace. |
organizationPermissions | string[] | Array of all RBAC permissions the user holds in the active organization. |
workspacePermissions | string[] | Array of all RBAC permissions the user holds in the active workspace. |
Methods
has(options: PermissionCheck)
A synchronous boolean check to assert if the current session meets specific contextual requirements.
protect(options?: ProtectOptions)
An asynchronous function that will throw a WachtAuthError (HTTP 403 Forbidden) if the specified conditions are not met.
Framework Adapters
While you can construct genericRequest objects, Wacht provides dedicated SDKs tailored for specific frameworks that wrap these functions directly around the framework’s native request/context pipelines:
- Next.js:
@wacht/nextjs/server - React Router:
@wacht/react-router/server
