Overview
Minimal’s API uses session-based authentication powered by Better Auth. All authenticated endpoints verify the user’s session before executing the requested operation.Authentication Flow
The authentication is handled by middleware defined inserver/context.ts:5:
Protected Procedures
Most API procedures require authentication using theauthed middleware:
server/context.ts:19 for the implementation.
Session Context
Authenticated procedures have access to the user context:How Sessions Work
Client-Side (Browser)
Sessions are automatically managed through HTTP-only cookies set by Better Auth:- Cookie is set on successful login
- Automatically sent with each API request
- No manual token management required
Server-Side
Each RPC request:- Extracts session from request headers
- Validates session with Better Auth
- Attaches user to context if valid
- Returns
UNAUTHORIZEDerror if invalid or missing
Public Endpoints
Some endpoints use thebase middleware instead of authed, making them accessible without authentication:
Error Responses
When authentication fails, the API returns anUNAUTHORIZED error:
Usage Examples
Client Component with Auth
Server Component with Auth
Resource Ownership
All authenticated procedures enforce resource ownership by filtering queries byuserId: