Overview
The Better Skills API uses Better Auth for authentication, providing:- Session-based authentication for web clients
- Device-code flow for CLI authentication
- Bearer token support via the bearer plugin
- Social OAuth (Google, GitHub)
Authentication Flow
Session Context
Every tRPC request creates a context that includes the current session:Protected vs Public Procedures
Public procedures allow unauthenticated access:Better Auth Configuration
Better Auth is configured inpackages/auth/src/index.ts:
Web Client Authentication
Login Flow
The web app uses session cookies for authentication:Session Management
Better Auth automatically manages session cookies:tRPC Client Setup
Include credentials in tRPC client for session cookies:CLI Authentication (Device-Code Flow)
The CLI uses the device authorization plugin for authentication without a browser redirect.Flow Overview
- CLI requests a device code
- User visits verification URL and enters the code
- CLI polls for authorization
- Upon success, CLI receives a bearer token
CLI Implementation
The CLI authenticates using Better Auth’s device authorization:CLI tRPC Client
Use the bearer token in the Authorization header:Admin API Authentication
For server-to-server communication, use the admin API token:Configuration
Set theBETTER_SKILLS_ADMIN_TOKEN environment variable:
Usage
Include the token in thex-better-skills-admin-token header:
Admin Procedures
Admin procedures are protected by token validation:Session User Access
In protected procedures, access the authenticated user viactx.session:
Auth Endpoints
Better Auth endpoints are mounted at/api/auth/*:
/api/auth/sign-in/social- Social OAuth sign-in/api/auth/sign-out- Sign out/api/auth/session- Get current session/api/auth/device/request- Request device code (CLI)/api/auth/device/token- Poll for token (CLI)
Security Considerations
- Cookies use
httpOnly,secure, andsameSite: "none"attributes - Cross-subdomain support enabled for multi-domain deployments
- Trusted origins configured via
CORS_ORIGINandBETTER_AUTH_URL - Account linking allowed for Google and GitHub providers
- Device authorization validates
clientId === "better-skills-cli"
Troubleshooting
Session Not Found
Ensure cookies are included in requests:CLI Token Expired
Re-authenticate using the device-code flow and update the stored token.CORS Errors
Verify the request origin matches the configuredCORS_ORIGIN or its www/non-www variant.