# Authentication

> Sign-in methods in TimeWren: email and password, magic link, passkeys, 2FA, SMS, OAuth providers, the first admin account and secret rotation.

Source: https://timewren.com/docs/configuration/authentication

TimeWren uses [Better Auth](https://www.better-auth.com). Email and password sign-in always works. Everything else is optional and switches on when you set the matching variables. All variables are listed in `.env.example`; see also [Environment variables](https://timewren.com/docs/configuration/environment.md).

## Email and password

Users sign up with email and password. Passwords are reset by mail (the link is valid for 1 hour).

Email verification is required by default: a new user must click the link in the verification mail (valid for 24 hours) before signing in. Verification mails need a configured mail transport, see [Email](https://timewren.com/docs/configuration/email.md).

You can switch verification off only when no mail transport is configured:

```dotenv
REQUIRE_EMAIL_VERIFICATION=false
```

The flag is ignored as soon as SMTP or Resend is set. The app logs a warning in that case.

> **Warning:** With verification off, anyone who can reach the instance can create an account with any address, including the address in `ADMIN_EMAIL`. Use it to get started, then configure mail and remove the line before exposing the instance.

Sign-in attempts are rate limited: 5 password sign-ins per 15 minutes, 10 sign-ups per 15 minutes.

## The first admin

The instance admin role is assigned automatically:

- If `ADMIN_EMAIL` is set (comma separated list), the account with that address becomes instance admin.
- If it is empty, the first account created becomes admin, but only while the instance has no admin yet.

Promotion happens at sign-up and at sign-in. When verification is required, only a verified address is promoted. Every new user also gets a personal workspace (an organization of which they are the owner).

```dotenv
ADMIN_EMAIL=you@example.com
```

## Magic link

Passwordless sign-in by mail. The link is valid for 15 minutes. It needs a working mail transport and has no setting of its own.

## Passkeys

Users can register passkeys (WebAuthn) under their security settings and sign in with them. Passkeys are bound to a domain, called the relying-party ID. By default it is the hostname of `NEXT_PUBLIC_APP_URL`, so the app must be served over HTTPS on its final domain before people register passkeys. If you change the domain, existing passkeys stop working.

Set `AUTH_RP_ID` only if you split the app and marketing site across hosts and want passkeys bound to the parent domain:

```dotenv
AUTH_RP_ID=example.com
```

## Two-factor authentication

Users can turn on two-factor authentication in their security settings. It supports:

- TOTP authenticator apps (6 digits, 30 second period)
- 10 backup codes
- A one-time code by mail as a fallback (needs a mail transport)

Two-factor verification attempts are rate limited to 10 per 15 minutes.

## SMS login

Sign-in with a phone number and a 6-digit code (valid for 5 minutes). Sending uses MessageBird:

```dotenv
MESSAGEBIRD_API_KEY=your-messagebird-key
MESSAGEBIRD_ORIGINATOR=TimeWren
```

Without `MESSAGEBIRD_API_KEY`, requesting a code fails with a "not configured" error. A user who signs up by phone only gets a placeholder email address on your mail domain (`EMAIL_DOMAIN`, defaulting to the marketing or app host). The text message itself is currently written in Danish.

## OAuth providers

Each provider needs an OAuth app that you register yourself. A provider is offered only when both its variables are set. The callback URL is always `{NEXT_PUBLIC_APP_URL}/api/auth/callback/<id>`.

| Provider | Id | Variables | Callback URL |
| --- | --- | --- | --- |
| Google | `google` | `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET` | `https://timewren.example.com/api/auth/callback/google` |
| GitHub | `github` | `GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET` | `https://timewren.example.com/api/auth/callback/github` |
| Discord | `discord` | `DISCORD_CLIENT_ID`, `DISCORD_CLIENT_SECRET` | `https://timewren.example.com/api/auth/callback/discord` |
| Apple | `apple` | `APPLE_CLIENT_ID`, `APPLE_TEAM_ID`, `APPLE_KEY_ID`, `APPLE_PRIVATE_KEY` | `https://timewren.example.com/api/auth/callback/apple` |
| Microsoft Entra ID | `microsoft-entra-id` | `MICROSOFT_CLIENT_ID`, `MICROSOFT_CLIENT_SECRET`, `MICROSOFT_TENANT_ID` | `https://timewren.example.com/api/auth/callback/microsoft-entra-id` |

Notes:

- The Google client is shared with the [Google Calendar integration](https://timewren.com/docs/integrations/calendar.md), which has its own callback URL.
- Sign in with Apple: `APPLE_CLIENT_ID` is the Services ID. The client secret is generated from your `.p8` key (`APPLE_PRIVATE_KEY`, PEM text with `\n` line breaks or the whole file base64 encoded) when the app starts. `APPLE_APP_BUNDLE_IDENTIFIER` (default `dk.timetrack.app`) is the iOS bundle id used for native sign-in.
- Microsoft: `MICROSOFT_TENANT_ID` defaults to `common` (any Microsoft account). Set a tenant GUID to restrict sign-in to one organization. See `docs/MICROSOFT-LOGIN-SETUP.md` in the repository for the Azure steps.
- Accounts from Google and GitHub are linked automatically when the email address matches an existing account. Discord accounts are only linked when Discord reports the email address as verified.

## Trusted origins

Browsers send requests from the origin of `NEXT_PUBLIC_APP_URL`, which is always trusted. If the app is also reached from another web origin (for example a second domain), add it:

```dotenv
TRUSTED_ORIGINS=https://timewren.example.org,https://time.example.com
```

The value is a comma separated list of `https://host` origins. Invalid entries are skipped.

## Sessions

Sessions last 7 days. An active session is extended at most once every 24 hours. The session cookie is host-only by default. `AUTH_COOKIE_DOMAIN` shares it across subdomains and `AUTH_COOKIE_PREFIX` changes its name (default `better-auth`); both are only needed with a split app and marketing host.

## Rotating BETTER_AUTH_SECRET

`BETTER_AUTH_SECRET` is the Better Auth secret. Among other things it encrypts the OAuth login tokens stored in the database, and changing it directly makes those tokens unreadable until users sign in again. To rotate it safely, use versioned secrets:

```dotenv
BETTER_AUTH_SECRETS=2:the-new-long-random-secret,1:the-old-long-random-secret
```

The format is `<version>:<secret>` separated by commas. The first entry is the current secret, used for new data. Keep older versions in the list for as long as you need to read data written with them. Versions are non-negative integers and must be unique. Use at least 32 characters per secret.

`BETTER_AUTH_SECRET` itself must stay set. The app also reads it directly to sign the older desktop device tokens and mail unsubscribe links, so changing its value invalidates those; users of the desktop app then sign in again.

`ENCRYPTION_KEY` is a separate key (it protects integration credentials) and is not affected by this rotation. Back both up.
