import { api } from "encore.dev/api";
// Protected endpoint - requires authentication
export const getProfile = api(
{ method: "GET", path: "/profile", expose: true, auth: true },
async (): Promise<Profile> => {
// Only authenticated users reach here
}
);
// Public endpoint - no authentication required
export const healthCheck = api(
{ method: "GET", path: "/health", expose: true },
async () => ({ status: "ok" })
);
| Scenario | Handler Returns | Result |
|---|---|---|
| Valid credentials | AuthData object | Request authenticated |
| Invalid credentials | Throws APIError.unauthenticated() | Treated as no auth |
| Other error | Throws other error | Request aborted |
| Endpoint Config | Request Has Auth | Result |
|---|---|---|
| auth: true | Yes | Proceeds with auth data |
| auth: true | No | 401 Unauthenticated |
| auth: false or omitted | Yes | Proceeds (auth data available) |
| auth: false or omitted | No | Proceeds (no auth data) |
Auth data automatically propagates to internal service calls:
You can explicitly override auth data when making service-to-service calls:
Mock authentication in tests using Vitest: