Skip to main content

Authentication

Better Auth session and platform identity on the Go API.
3 min read

API auth is based on the Better Auth session Bearer (the Next BFF forwards it) for product routes and writes. /api/internal/* routes and cacheable product reads from the BFF use a shared secret (CRON_SECRET) server-to-server. Public Metronome and QStash callbacks do not use Bearer: they validate their signature over the exact bytes before processing the event.

Layers#

LayerMechanismWho validates itTypical failure
UserAuthorization: Bearer <session token>ResolvePlatformIdentity + RequireUserAuth / RequireAuth401 authentication required / invalid session
BFF internal readAuthorization: Bearer <CRON_SECRET> on GET/HEAD with :profileIdResolvePlatformIdentity (constant-time secret)401 (non-safe method, no profile, invalid secret)
ProfilePath :profileId + membership (session) or membership already asserted in Next (internal)Session: middleware; Internal: BFF before cache403 profile access denied
Sectionmember section_access (session) or requireGoDomainServerAccess(ws, section) in Next (internal)Session: middleware; Internal: BFF403 section access denied

There are no X-Agata-* identity headers. With a session, the user comes only from the token; the profile comes only from the path. With internal BFF, there is no user id in Go: assignedToMe / createdByMe filters require a session.

Better Auth session#

Go resolves the token against the Better Auth session table and links it to users via auth_user_id.

Mirror Better Auth → users#

  1. On user.create, the BFF calls POST /api/internal/users/bootstrap with Authorization: Bearer <CRON_SECRET> (body with authUserId). Without the secret → 401.
  2. If the mirror is still missing, GET /users/me returns 401 and Next calls POST /users/me/bootstrap with the Better Auth session Bearer.
  3. Authenticated user bootstrap (/users/me/bootstrap) requires a valid session token (a loose auth-user header is no longer accepted).

Profile in the URL#

When the route includes :profileId, the middleware checks membership and section_access. The profile is not sent as a header.

Which middleware applies#

GroupExtra middlewareExample
/webhooks/metronomeDate signature + HMAC SHA-256Idempotent balance/contract notifications
/qstash/*HS256 JWT Upstash-Signature, exact subject, and SHA-256 hashIdempotent dispatch and failure callback
/internal/*CronAuthcrons, bootstrap, Stripe sync, and QStash recovery
/users/*RequireUserAuthGET /users/me
/profiles/*RequireUserAuth (+ internal OK if :profileId is present)members, usage, and Agent credit reservations
/{domain}/profiles/:id/*RequireAuth (user+profile+section or internal BFF)tasks, finance, notebook…

Reserving, confirming, and releasing Agent credits requires a session and profile membership. The hourly job POST /internal/qstash/recover and the operational dispatch/reconciliation endpoints require CRON_SECRET. The callbacks POST /qstash/notifications/dispatch, POST /qstash/metronome/dispatch, and POST /qstash/failure are registered before platform identity and only accept a valid QStash signature.

curl — internal (CRON_SECRET)#

Examples#

curl — health (no auth)#

curl — current user#

TypeScript — BFF#