@revstackhq/auth package provides a unified JWT verification system that works with multiple authentication providers. It handles both RS256 (public key) and HS256 (shared secret) signing strategies.
TokenVerifier
TheTokenVerifier class validates JWT tokens against a stored authentication contract.
Constructor
The authentication contract defining verification parameters (strategy, JWKS URI, issuer, etc.)
Optional configuration for JWKS caching and timeouts
TokenVerifierOptions
Options passed to the underlying jose library’s
createRemoteJWKSet for cache control and timeoutsMethods
verify
Validates a raw JWT string and returns a session object with user claims.The JWT token string to verify
Promise<RevstackSession<T>> - Session object containing user ID, claims, and validation status
Types
RevstackSession
The session object returned byverify():
The authenticated user ID extracted from the token (from
sub or custom claim)The raw decoded JWT payload containing all token claims
Whether the token passed validation. Check this before trusting
userId or claimsHuman-readable error message if
isValid is falseSpecific error code if
isValid is false. See Error CodesRevstackAuthContract
Union type representing either RS256 or HS256 authentication configuration.RS256AuthContract
Used for providers like Auth0, Clerk, Firebase that use public key cryptography:Provider identifier:
"auth0" | "clerk" | "supabase" | "cognito" | "firebase" | "custom"Discriminator for RS256 verification via remote JWKS
The JSON Web Key Set URI to verify signatures (e.g.,
https://your-domain.auth0.com/.well-known/jwks.json)Expected issuer (
iss) claim valueExpected audience (
aud) claim valueClaim holding the unique user ID. Defaults to
"sub" if not specifiedHS256AuthContract
Used for custom JWTs or providers that support shared secrets:Provider identifier:
"auth0" | "clerk" | "supabase" | "cognito" | "firebase" | "custom"Discriminator for HS256 verification via shared secret
Shared signing secret used to verify signatures
Expected issuer (
iss) claim, if enforcedExpected audience (
aud) claim, if enforcedClaim holding the unique user ID. Defaults to
"sub" if not specifiedError Codes
The token has expired (based on
exp claim)The token signature verification failed
The
iss claim doesn’t match the expected issuerFailed to fetch JWKS from the remote endpoint (RS256 only)
The required user ID claim is missing from the token
The token has malformed JWT structure or failed claim validation
An unexpected error occurred during verification
Provider Input Types
Helper types for creating auth contracts for specific providers:Auth0Input
Auth0 domain, with or without protocol (e.g.,
"my-tenant.us.auth0.com")Expected audience (
aud)Optional override for user ID claim
ClerkInput
Clerk issuer URL (e.g.,
"https://clerk.your-domain.com")Optional override for user ID claim
SupabaseInput
Supabase project URL (e.g.,
"https://xyzcompany.supabase.co")Optional JWT signing secret for HS256 verification. If provided, the contract becomes HS256
Optional expected audience (
aud)Optional override for user ID claim
FirebaseInput
Firebase project ID (e.g.,
"my-firebase-project")Optional override for user ID claim
CognitoInput
AWS region (e.g.,
"us-east-1")Cognito User Pool ID (e.g.,
"us-east-1_Abc123")Cognito App Client ID (used as
aud)Optional override for user ID claim
CustomJwtInput
HS256 signing secret
Optional expected issuer (
iss)Optional expected audience (
aud)Optional override for user ID claim
Example Usage
Auth0 Integration
Supabase with HS256
Error Handling
Best Practices
Cache TokenVerifier instances: Create one verifier per auth provider and reuse it across requests. The JWKS cache is managed internally.