Installation
Configuration
Get Clerk Credentials
Sign up at Clerk and create an application. Get your:
- Secret Key
- Publishable Key
- JWKS URI (from the API Keys page)
Configuration Options
Clerk secret key. Defaults to
process.env.CLERK_SECRET_KEYClerk publishable key. Defaults to
process.env.CLERK_PUBLISHABLE_KEYJWKS URI for token verification. Defaults to
process.env.CLERK_JWKS_URIProvider name identifier
Usage Examples
Basic Setup
Explicit Configuration
Custom Authorization
Client Integration
React with Clerk
Making Authenticated Requests
Token Verification
Mastra automatically verifies Clerk JWT tokens:- Token Extraction: Bearer token from
Authorizationheader - JWKS Verification: Validates token signature using Clerk’s JWKS endpoint
- Claims Validation: Checks token expiration and issuer
- Authorization: Calls custom
authorizeUserfunction if provided
User Object
The authenticated user object contains standard JWT claims:Authentication Flow
Best Practices
Use Environment Variables
Store Clerk credentials in environment variables, never hardcode them.
Enable Email Verification
Require email verification in your authorization logic for added security.
Token Expiration
Tokens expire automatically. Use Clerk’s session management to refresh tokens.
Production Keys
Use separate Clerk applications for development and production.
Clerk Features
Pre-built Components
Clerk provides ready-to-use UI components:- Sign In / Sign Up forms
- User profile management
- Multi-factor authentication
- Social OAuth providers
Advanced Features
- Organizations: Multi-tenant support
- Roles & Permissions: Fine-grained access control
- Sessions: Device-aware session management
- Webhooks: Real-time user event notifications
Troubleshooting
Invalid JWKS URI
Ensure your JWKS URI matches your Clerk domain:Token Verification Failed
Check that:- Token is not expired
- JWKS URI is correct
- Secret key matches your Clerk application
Missing Authorization Header
Ensure client includes Bearer token:Related Resources
Auth0
Alternative enterprise identity platform
Supabase Auth
Open-source authentication
Clerk Documentation
Official Clerk documentation
Clerk Dashboard
Manage applications and users