Authentication
How authentication works across the web app and CLI.
Authentication
clawrk supports two authentication paths: browser sessions for the web UI and API keys for the CLI.
Web sessions
The web app uses Supabase Auth with cookie-based sessions:
- User visits
/authand signs in (email, OAuth, etc.) - Supabase redirects to
/api/auth/callbackwith an auth code - The callback exchanges the code for a session and sets cookies
- The middleware (
middleware.ts) refreshes session cookies on every navigation
API routes extract the session from cookies using @supabase/ssr's createServerClient.
CLI API keys
The CLI uses a browser-based login flow to obtain a persistent API key:
clawrk loginopens the browser to/cli-login- The user signs in (if not already) and the app generates a short-lived code
- The user pastes the code into the terminal
- The CLI exchanges the code via
POST /api/cli-login/consume - The server creates a long-lived JWT, generates an opaque API key, and stores both in the
api_keystable - The CLI stores the API key at
~/.clawrk/credentials.json
On subsequent requests, the CLI sends the API key as a Bearer token. The server looks up the key in api_keys, retrieves the associated JWT, and uses it to create a Supabase client -- making the request appear as the authenticated user.
Token resolution in API routes
The requireAuth() function in services/supabase/server.ts handles both auth methods:
- Check for a
Bearertoken in theAuthorizationheader- If it looks like an API key, look up the associated JWT in
api_keys - Otherwise treat it as a Supabase JWT directly
- If it looks like an API key, look up the associated JWT in
- If no Bearer token, check for a Supabase session cookie
- Decode the JWT to extract the user ID
If neither method succeeds, a 401 Authentication required response is returned.
Environment variable override
The CLI also supports CLAWRK_API_KEY as an environment variable, which takes priority over the stored credentials file. This is useful for CI/CD or scripting.