Skip to content

Authentication

All sign-in runs on cemco-auth (better-auth on D1 cemco-auth), at auth.cemco.app and, on the app host, behind the same-origin proxy app.cemco.app/auth. Every auth route lives under /auth/* so the two are identical.

Who How Role comes from
Staff Google sign-in limited to the Workspace domain (STAFF_EMAIL_DOMAIN, default cemcoinc.com), or a one-time code by email users.role in the CRM. A first sign-in from ADMIN_EMAILS becomes admin; anyone else starts as viewer
Customers Email and password, or a magic link, after accepting a staff invite customer_users, linking the login to exactly one account
Scripts and agents API key in x-api-key The key’s staff role (default editor), set by the admin who created it
MCP clients (Claude) OAuth 2.1 with dynamic client registration; the user signs in and consents The signed-in staff user’s role

Session cookies are scoped to COOKIE_DOMAIN (.cemco.app in production), so every subdomain shares one sign-in; leave it empty on localhost and on a single workers.dev host. Logins are never created by sign-up: customers come from a staff invite, staff from Google or an email code on the staff domain, and the very first admin can come from the one-time bootstrap below.

Each method answers not connected instead of failing when its configuration is missing: Google without GOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET, email codes, sign-in links and password resets without email sending (EMAIL_ENABLED), OAuth for MCP clients without both AUTH_URL and MCP_URL, and everything without BETTER_AUTH_SECRET outside local development.

  1. A Worker receives a request and passes its auth headers (cookie, authorization, x-api-key) to env.AUTH.resolve(pickAuthHeaders(request.headers)).
  2. AUTH returns an identity (who signed in, and how) or null.
  3. env.API.actorFor(identity) maps it to an actor with a CRM role, provisioning a first-time staff user. A customer identity without an active customer_users row gets no actor.
  4. The operation runs as that actor.

There is no binding from AUTH to API (it would be circular). Invite acceptance therefore goes POST api.cemco.app/v1/portal/invites/accept → system operation portal_invites.accept → env.AUTH.createCustomerUser.

Staff create an invite (portal_invites.create). The customer gets an email with a link valid for 14 days. Opening it looks up the token (GET /v1/portal/invites/:token), the customer sets a password, and acceptance creates the login and links it to the account. Staff can revoke invites and turn portal logins off.

Admins create keys with users.create_api_key and revoke them with users.revoke_api_key. The secret is shown once. Send it as x-api-key. Keys are for scripts and agents; every write they make is audited under the key.

If Google is not set up yet, set the BOOTSTRAP_TOKEN secret and open /auth/bootstrap: it creates one admin login with an email and password. It stops working as soon as any staff login exists; remove the secret afterwards.

Staff can grant Gmail (read sent mail, for email filing) and Google Calendar (for calendar sync) at /auth/connect/google. The tokens are stored encrypted in cemco-auth; other Workers ask for a fresh access token over the AUTH binding.

Only when ENVIRONMENT is exactly development (set in workers/auth/.dev.vars and nowhere else): /auth/dev lets you pick a login, and the x-cemco-dev-user: <email> header signs a request in. In any other environment the routes return 404 and the header and cookie are ignored; a test covers this. See Local development.

BETTER_AUTH_SECRET (signs sessions and encrypts stored Google tokens), GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET (staff Google sign-in; the redirect URL is <AUTH_URL>/auth/callback/google), and the one-time BOOTSTRAP_TOKEN. Without the Google pair the sign-in page shows Google as not connected and email codes still work once email sending is on. Every setting is explained in workers/auth/.dev.vars.example.