Skip to content

Authentication

Authentication runs on Better Auth inside the Worker. It stores its data in D1 through the Drizzle adapter. Its endpoints live under /api/auth. The Auth REST API reference lists them all.

Client How it signs in
Web and PWA A session cookie on the host of your app (BETTER_AUTH_URL). You can sign in with Google, GitHub or a passkey, or use a guest account. You can upgrade a guest account later by linking a provider. If the landing page is on a separate host, COOKIE_DOMAIN shares the cookie with the parent domain. gymgym.club does this.
CLI The device code flow. gymgym login shows a code. You approve the code at /device while you are signed in. The CLI then keeps a bearer token. Only first-party client ids (DEVICE_CLIENTS in packages/api/src/auth/plugins.ts) can start this flow.
Native apps and watches (later) A bearer token from the same device flow, or a token that the phone app hands over.
Telegram Mini App (later) A Better Auth plugin that checks the signed initData of Telegram.

Set these Worker secrets to turn on a provider. Without them, the button is hidden. A callback URL is the address of your app (BETTER_AUTH_URL) plus the path below. Here, https://app.example.com stands for that address.

Provider Secrets Callback URL
Google GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET https://app.example.com/api/auth/callback/google
GitHub GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET https://app.example.com/api/auth/callback/github
Cloudflare (optional) ENABLE_CLOUDFLARE_LOGIN=1, CLOUDFLARE_CLIENT_ID, CLOUDFLARE_CLIENT_SECRET https://app.example.com/api/auth/callback/cloudflare

GitHub allows one callback URL for each OAuth app, so a staging copy and local development each need their own app. Google accepts several callback URLs on one client.

The buttons stay hidden until you set both secrets of a provider. You can do this at any time after the deploy. Email and password sign-in is off in production (see below). Without a provider, people can still use a guest account or a passkey.

  1. Google: In the Google Cloud Console, open Credentials and create an OAuth client ID of type Web application. Under Authorized redirect URIs, add https://app.example.com/api/auth/callback/google. If you develop locally, also add http://localhost:4321/api/auth/callback/google. Copy the client ID and the secret.
  2. GitHub: Under Settings, Developer settings, OAuth Apps, create a New OAuth App. Set the Authorization callback URL to https://app.example.com/api/auth/callback/github. Make a second app for local development if you need one.
  3. Save the four secrets with npx wrangler secret put NAME from apps/web. Add --env staging for a staging copy. The names are GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GITHUB_CLIENT_ID and GITHUB_CLIENT_SECRET. Each command asks for the value, so no secret goes into the repository.
  4. Open the login page. The Google and GitHub buttons appear. Sign in with each one once. A guest can also link them later in Settings, Sign-in methods. This keeps the data of the guest.

A passkey belongs to a relying party id, PASSKEY_RP_ID. If you do not set it, gymgym uses the host of BETTER_AUTH_URL. That is right for a single address. Set it to your registrable domain (example.com) when several hosts of that domain must share passkeys. An example is an app host and a staging host. Choose the id before anyone registers a passkey, because a later change invalidates all passkeys. The id must be the address that people use, or a parent of it. If not, browsers refuse the passkey.

An account gets the admin role when it signs up or signs in and its email is in ADMIN_EMAILS or its user id is in ADMIN_USER_IDS. Both are comma-separated lists. To make an existing account an admin, add it to a list. It gets the role at its next sign-in. If you remove someone from a list, they keep the role. With INVITE_ONLY=1, a new account needs an invite code that an admin created.

Variable Effect
PASSWORD_LOGIN=1 Staging or production also accept an email and a password. Sign-up with a password stays off. A signed-in user adds a password in Settings (the setPassword mutation) and changes it with /api/auth/change-password. This way, nobody can register an address that they do not control. It also stops Google or GitHub linking by email from giving such an account to the first person who registered the address. If someone forgets the password, they sign in the way they first did and set a new password.
ALLOW_GUEST=0 Turns off guest accounts. New passkey-only accounts start as guests, so they stop too. Existing passkeys keep working.
DEFAULT_LANG=de The language for visitors and new accounts whose browser asks for none of the supported languages. The default is English.

In local development (ENVIRONMENT=development), email and password sign-in and sign-up are always on, for the seed accounts and for tests.

In Settings, you can rename or remove each passkey. You cannot remove the last way into a guest account.

Every new session adds a row to sign_in. The row holds the time, the method (google, github, passkey, password, guest or cli), the country from the cf-ipcountry header of Cloudflare and a short device label, such as “Firefox · macOS”. gymgym does not store IP addresses or full user agents. Admins read the log on the dashboard (admin.signIns). The hourly cron deletes rows older than 90 days.

The rate limiter of Better Auth is on in staging and production and off locally, so tests and development are not slowed down.