Overview
Local token authentication uses a single shared bearer token for all API requests. This mode is designed for:- Self-hosted deployments with trusted network access
- Development and testing environments
- Single-user or single-team deployments
Configuration
Backend Configuration
Set the following environment variables in your backend.env file:
backend/.env
Must be set to
local to enable local token authenticationThe shared bearer token used for all API requests. Must be at least 50 characters long.Generate a secure token with:
Frontend Configuration
Set the following environment variables in your frontend.env.local file:
frontend/.env.local
Must be set to
local to enable local token mode in the frontendMust match the backend
LOCAL_AUTH_TOKEN exactly. The frontend uses this to authenticate automatically without a login screen.The frontend will also check
sessionStorage["mc_local_auth_token"] if the environment variable is not set.Authentication Flow
Frontend Auto-Authentication
Frontend automatically includes the token in all API requests via the
Authorization headerBackend Token Validation
Backend extracts token from
Authorization: Bearer <token> header and performs constant-time comparison against LOCAL_AUTH_TOKENUser Context Resolution
On successful authentication, backend returns or creates the local admin user with:
clerk_user_id:local-auth-useremail:[email protected]name:Local User
API Usage
Making Authenticated Requests
Include the token in theAuthorization header:
Bootstrap User Context
Token Validation Details
Backend Implementation
The backend validates tokens inapp/core/auth.py:_resolve_local_auth_context():
- Extract token from
Authorization: Bearer <token>header - Validate format: Token must be present and non-empty
- Compare tokens: Uses
hmac.compare_digest()for constant-time comparison to prevent timing attacks - Return user: On success, returns or creates the local admin user
Security Properties
Constant-time comparison prevents timing attacks that could leak token information
Token length requirement (50+ characters) ensures sufficient entropy
Error Handling
Missing Token
Request:401 Unauthorized
Invalid Token
Request:401 Unauthorized
Malformed Authorization Header
Request:401 Unauthorized (missing “Bearer” prefix)
Common Issues
Token Too Short
Frontend/Backend Token Mismatch
CORS Errors
Migrating to Clerk
To migrate from local token to Clerk authentication:Set up Clerk account
Create a Clerk account at clerk.com
Best Practices
Generate strong tokens: Use cryptographically secure random token generators
Rotate tokens regularly: Update
LOCAL_AUTH_TOKEN periodically in secure deploymentsRestrict network access: Use firewall rules or VPN to limit API access
Use HTTPS: Always use HTTPS in production to prevent token interception
Next Steps
Clerk Authentication
Upgrade to multi-user JWT authentication
Agent Tokens
Learn about agent authentication