@protobase/cli 0.22.13 → 0.23.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +2 -0
- package/dist/package.js +1 -1
- package/dist/skill/install.js +29 -0
- package/dist/skill/register.js +14 -0
- package/package.json +8 -7
- package/skills/protobase/SKILL.md +89 -0
- package/skills/protobase/references/getting-started.md +78 -0
- package/skills/protobase/references/guides/api-functions.md +240 -0
- package/skills/protobase/references/guides/billing-page.md +288 -0
- package/skills/protobase/references/guides/create-page.md +108 -0
- package/skills/protobase/references/guides/deploy.md +52 -0
- package/skills/protobase/references/guides/export-a-list.md +44 -0
- package/skills/protobase/references/guides/field-type.md +131 -0
- package/skills/protobase/references/guides/first-composed-page.md +230 -0
- package/skills/protobase/references/guides/first-plugin.md +166 -0
- package/skills/protobase/references/guides/import-records.md +43 -0
- package/skills/protobase/references/guides/plugin-build.md +64 -0
- package/skills/protobase/references/guides/record-card.md +70 -0
- package/skills/protobase/references/guides/remote-resources.md +185 -0
- package/skills/protobase/references/guides/table-to-admin.md +205 -0
- package/skills/protobase/references/guides/theme-an-app.md +87 -0
- package/skills/protobase/references/reference/access.md +115 -0
- package/skills/protobase/references/reference/ai.md +106 -0
- package/skills/protobase/references/reference/api.md +231 -0
- package/skills/protobase/references/reference/architecture.md +63 -0
- package/skills/protobase/references/reference/assistant.md +157 -0
- package/skills/protobase/references/reference/auth.md +462 -0
- package/skills/protobase/references/reference/blocks.md +341 -0
- package/skills/protobase/references/reference/bottom-bar.md +44 -0
- package/skills/protobase/references/reference/cli.md +493 -0
- package/skills/protobase/references/reference/client.md +38 -0
- package/skills/protobase/references/reference/comments.md +83 -0
- package/skills/protobase/references/reference/custom-components.md +287 -0
- package/skills/protobase/references/reference/data-config.md +266 -0
- package/skills/protobase/references/reference/email.md +166 -0
- package/skills/protobase/references/reference/events.md +121 -0
- package/skills/protobase/references/reference/files.md +291 -0
- package/skills/protobase/references/reference/forms.md +133 -0
- package/skills/protobase/references/reference/history.md +86 -0
- package/skills/protobase/references/reference/jobs.md +174 -0
- package/skills/protobase/references/reference/layouts.md +126 -0
- package/skills/protobase/references/reference/plugins.md +328 -0
- package/skills/protobase/references/reference/remote-resources.md +198 -0
- package/skills/protobase/references/reference/sidebar.md +51 -0
- package/skills/protobase/references/reference/theme.md +132 -0
- package/skills/protobase/references/reference/ui-config.md +880 -0
- package/skills/protobase/references/reference/user-menu.md +38 -0
- package/skills/protobase/references/reference/versioning.md +91 -0
- package/skills/protobase/references/reference/webhooks.md +136 -0
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
<!-- Generated by scripts/protobase-skill.ts from website/src/content/docs/reference/auth.md: run `bun run docs:skill`, do not edit. -->
|
|
2
|
+
|
|
3
|
+
# Login with Better Auth
|
|
4
|
+
|
|
5
|
+
Sign-in with Better Auth, with the admin store, sign-in methods and policy, sign-in providers, staff sign-in, roles, tokens and the public endpoints.
|
|
6
|
+
|
|
7
|
+
`@protobase/server` ships a [Better Auth](https://www.better-auth.com) setup (version 1.7): sign-in with a password, an [emailed code](#emailed-sign-in-codes) or a [passkey](#passkeys), [two-factor authentication](#two-factor-authentication), a [sign-in policy](#sign-in-policy) admins set in the app, [staff sign-in](#staff-sign-in) for the team that runs the app, [SAML single sign-on](#sign-in-with-saml), roles, and short-lived JWTs that the API verifies against Better Auth's own JWKS, and, when turned on, [organizations](#organizations) people work in one at a time. All of it is Better Auth's own plugins (`emailOTP`, `twoFactor`, `@better-auth/passkey`, `@better-auth/sso`), wired by `createAuth`.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { betterAuthAuthenticator, createAdmin, createAuth } from '@protobase/server'
|
|
11
|
+
|
|
12
|
+
const auth = createAuth({ database: pool, baseURL: 'https://admin.example.com', secret: process.env.BETTER_AUTH_SECRET! })
|
|
13
|
+
const admin = createAdmin({ resources, db, auth, authenticate: betterAuthAuthenticator({ auth, tenant: 1 }) })
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
`createAdmin({ auth })` mounts Better Auth at `/api/auth/*` next to the API at `/api/v1`.
|
|
17
|
+
|
|
18
|
+
## Admin store
|
|
19
|
+
|
|
20
|
+
Users, sessions and signing keys stay apart from the application's tables. The ERP example keeps them in an `auth` schema of its own database, so a host that gives an application one database (and a role that cannot create databases) is enough:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
createAuth({ database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: 'auth', transaction: true }, ... })
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Build that pool as `handleDroppedConnections(new pg.Pool(...))` (from `@protobase/server`) so a database restart is printed instead of stopping the app.
|
|
27
|
+
|
|
28
|
+
### Migrations
|
|
29
|
+
|
|
30
|
+
The store's tables are part of the project's migrations, committed in the repository like the application's own: the examples carry them as `db/migrations/*_auth.sql`, and their `db:migrate` applies both. When an upgrade of Protobase (or another Better Auth plugin) needs more tables or columns, write the change as the project's next migration:
|
|
31
|
+
|
|
32
|
+
```sh
|
|
33
|
+
bun run db:migrate # the database at the project's current schema
|
|
34
|
+
bun run protobase auth migration # compares it with what Better Auth needs; writes db/migrations/NNN_auth.sql
|
|
35
|
+
bun run db:migrate # applies it
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The file only adds tables, columns and indexes, never drops one, and is plain SQL to review, edit or leave out where the project handles the auth schema itself. `--dir` and `--name` choose the folder and the name after the number. [`protobase migration`](cli.md#migration)) writes the same file, along with what the config's other features need, such as [jobs](jobs.md)).
|
|
39
|
+
|
|
40
|
+
The store's tables and columns are snake_case, like the app's (`sign_in_policy.platform_sign_in`, `session.user_id`); the code and the API keep Better Auth's camelCase names. A store set up by Protobase 0.6 has Better Auth's camelCase names instead (`"signInPolicy"."platformSignIn"`): for it, `protobase auth migration` writes the migration that renames its tables, columns, indexes and constraints in place, keeping every row. The examples carry it as `db/migrations/*_auth_snake_case.sql`; on a store with the snake_case names it does nothing.
|
|
41
|
+
|
|
42
|
+
The server never changes the schema. `protobase serve` and `protobase dev` read it at startup and refuse to start while it lacks a table or column Better Auth needs, naming them and `protobase auth migration`; without that check every sign-in would fail (`500`, "Database schema mismatch"). The examples' `db:migrate` applies all pending files in one transaction, so a migration that fails leaves the database as the running version expects it. The host must guarantee the rest: nothing but the migrations writes while they run, and the new version serves only after they succeeded.
|
|
43
|
+
|
|
44
|
+
Upgrading from 0.5 or earlier, where `auth:migrate` created the tables, goes through 0.6. With Protobase 0.6, run `protobase auth migration` twice, once with `DATABASE_URL` on an empty database with only the project's own migrations applied (it writes the whole store), then on the project's database (it writes what this version adds). Every statement skips what exists already, so both files apply to new databases and to those `auth:migrate` set up. The `auth:migrate` script and `db/auth-migrate.ts` can then go, and the upgrade goes on as from 0.6.
|
|
45
|
+
|
|
46
|
+
`createAuth` takes a `pg` Pool or `{ dialect, type: 'postgres', schemaName? }` as `database`; without `schemaName` the tables go to the connection's `search_path` (usually `public`). The examples keep the store in the `auth` schema of `DATABASE_URL`'s database, so their migrations cover both.
|
|
47
|
+
|
|
48
|
+
## Configuration
|
|
49
|
+
|
|
50
|
+
| Variable | |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `BETTER_AUTH_SECRET` | signs cookies and encrypts the token keys; `openssl rand -base64 32`; never committed (`.env.example` has a placeholder) |
|
|
53
|
+
| `BETTER_AUTH_URL` | public URL of the API (default `http://localhost:5173`); issuer and audience of the tokens; when tunnelling, the https tunnel URL |
|
|
54
|
+
| `TRUSTED_ORIGINS` | extra comma separated origins; a dev tunnel needs its exact host here, such as `https://my-tunnel.trycloudflare.com`, since no wildcard is trusted |
|
|
55
|
+
| `PROTOBASE_ORIGINS` | the other origins the app is served on, comma separated, passed by the platform; see [Several origins](#several-origins) |
|
|
56
|
+
| `PROTOBASE_PASSKEY_DOMAIN` | the host new [passkeys](#passkeys) are made for, passed by the platform; default the host of `BETTER_AUTH_URL` |
|
|
57
|
+
| `PROTOBASE_CORS_ORIGINS` | other websites whose browser code may call the API, `*` or comma separated origins, passed by the platform; see [CORS](api.md#cors)) |
|
|
58
|
+
| `PROTOBASE_SMTP_URL`, `PROTOBASE_MAIL_FROM` | the mail server and sender for [password reset](#password-reset), [emailed sign-in codes](#emailed-sign-in-codes), emailed [two-factor](#two-factor-authentication) codes and [open sign-up](#open-sign-up), passed by the platform; all four are off without them |
|
|
59
|
+
| `PROTOBASE_APP_NAME` | the app's name, passed by the platform: the default of `appName` |
|
|
60
|
+
| `PROTOBASE_SIGN_IN_ISSUER`, `PROTOBASE_SIGN_IN_CLIENT_ID`, `PROTOBASE_SIGN_IN_CLIENT_SECRET` | the [platform sign-in provider](#platform-sign-in-provider) for people who have an account, passed by the platform; off without them. `PROTOBASE_SIGN_IN_PROVIDER` and `PROTOBASE_SIGN_IN_NAME` are optional |
|
|
61
|
+
| `PROTOBASE_OPERATOR_ISSUER`, `PROTOBASE_OPERATOR_CLIENT_ID`, `PROTOBASE_OPERATOR_CLIENT_SECRET` | the [operator provider](#operator-provider) its staff sign in with to sign in as people, passed by the platform; staff sign-in is off without them. `PROTOBASE_OPERATOR_NAME` and `PROTOBASE_OPERATOR_GROUP` are optional |
|
|
62
|
+
|
|
63
|
+
- Cookies are `SameSite=Lax`, and `Secure` when `BETTER_AUTH_URL` is https.
|
|
64
|
+
- Sign-in is rate limited (5 password attempts per minute and client by default, `signInPerMinute`, which also limits [sign-up](#open-sign-up) requests; 3 per minute for emailed codes); the limiter keys on `x-forwarded-for`, so run behind a proxy that sets it.
|
|
65
|
+
- Passwords need at least 12 characters.
|
|
66
|
+
- `appName` is the app's name: authenticator apps and passkey prompts show it, the admin app's sidebar shows it at the top beside Protobase's mark (Protobase's wordmark when it is `Protobase`), every browser tab of the admin app ends its title with it, the [auth emails](email.md#protobases-own-mail)) name the app with it, and the platform's mailer sends them as it (the sender's display name). Default `PROTOBASE_APP_NAME`, when set, and `Protobase` otherwise, then without a display name.
|
|
67
|
+
|
|
68
|
+
## First run
|
|
69
|
+
|
|
70
|
+
Accounts are never created over the web while the store is empty: there is no setup route, and [open sign-up](#open-sign-up), when an app turns it on, waits for the first admin, so a freshly deployed URL cannot be claimed by whoever reaches it first. The first admin is created on the host, with the CLI (`protobase users create you@example.com --generate-password`) or from code:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { createUser, hasUsers, listUsers } from '@protobase/server'
|
|
74
|
+
|
|
75
|
+
await createUser(auth, { email, password, name?, role? }) // the first user is always an admin; later ones default to `user`
|
|
76
|
+
await hasUsers(auth) // boolean
|
|
77
|
+
await listUsers(auth) // [{ id, email, role, banned, createdAt }], never passwords
|
|
78
|
+
await setUserRole(auth, { email, role }) // 'admin' | 'user'
|
|
79
|
+
await setUserBanned(auth, { email, banned }) // banning ends the user's sessions and blocks sign-in
|
|
80
|
+
await deleteUser(auth, email) // with sessions and accounts
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The last active admin cannot be deleted, demoted or banned: those functions throw instead, and Better Auth's admin endpoints answer `400 LAST_ADMIN`, so every caller is protected. An account created by `createUser` or an admin counts as having a verified address, since whoever created it vouches for it; that lets it sign in with an [emailed code](#emailed-sign-in-codes).
|
|
84
|
+
|
|
85
|
+
Until a user exists sign-in fails, and `GET /api/auth/status` (public) answers `{ "needsAdmin": true, "signInMethods": ["password", "passkey"], "passwordReset": false, "socialProviders": [] }` so a login page can say what to do. `signInMethods` lists the ways to sign in the [sign-in policy](#sign-in-policy) leaves on (`password`, `emailCode`, `passkey`), `passwordReset` is whether [reset mail](#password-reset) can be sent and passwords are on, `signUp`, when there, says anyone can [make an account](#open-sign-up), and `staffSignIn`, when there, names the provider of [staff sign-in](#staff-sign-in). Further users are created by an admin on the [Users page](#users-page), or with `createUser` on the host. The auth store holds a connection pool; scripts that call these functions end the process themselves.
|
|
86
|
+
|
|
87
|
+
## Users page
|
|
88
|
+
|
|
89
|
+
Admins manage accounts without the command line on the Users page, an ordinary [composed page](layouts.md)) the config imports: everyone who can sign in, found by address, with their roles to tick, Block and Unblock, inviting someone by email, and adding an account directly.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
// config/index.ts
|
|
93
|
+
export { usersPage } from '@protobase/layout' // at /accounts, for admins
|
|
94
|
+
// config/user-menu.ts
|
|
95
|
+
export const accountMenu = userMenu((m) => [m.page('accounts', { label: 'Users', icon: 'users' })])
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
New apps have it already. Leave the export out to drop it, or make a page of your own around the [`Users` block](blocks.md#users)). It works with or without [organizations](#organizations): an invitation to the app makes an account with the roles the admin ticked, from the same link page and sign-up as an organization's invitation, and someone with an account just signs in. Listing, creating, roles and blocking are Better Auth's admin endpoints (`/api/auth/admin/list-users`, `create-user`, `set-role`, `ban-user`, `unban-user`), whose roles go as a list; the rest are Protobase's, for admins only:
|
|
99
|
+
|
|
100
|
+
| Endpoint | Does |
|
|
101
|
+
| --- | --- |
|
|
102
|
+
| `POST /api/auth/users/invite` | `{ email, roles }`: invites an address without an account, for 7 days (`invitationDays` with organizations). Inviting it again replaces the invitation. Without a mailer the answer carries the `link` to pass on. |
|
|
103
|
+
| `GET /api/auth/users/invitations` | The pending invitations to the app: `{ invitations: [{ id, email, roles, expiresAt }] }`. |
|
|
104
|
+
| `POST /api/auth/users/invitations/cancel` | `{ id }`: revokes one, so its link stops working. |
|
|
105
|
+
| `GET /api/auth/users/roles` | Every role with its label, to choose from. |
|
|
106
|
+
|
|
107
|
+
An invitation to the app is a row of Better Auth's `verification` table, so it needs no table of its own. The client has these as `session.users`.
|
|
108
|
+
|
|
109
|
+
## Password reset
|
|
110
|
+
|
|
111
|
+
With mail configured, the sign-in page offers "Forgot password?": the person enters their email, gets a link, and chooses a new password on the page the link opens. It is Better Auth's own reset: `POST /api/auth/request-password-reset` with `{ email, redirectTo }`, the emailed link `GET /api/auth/reset-password/<token>`, which sends the browser back to `redirectTo` with `?token=...` (or `?error=INVALID_TOKEN`), and `POST /api/auth/reset-password` with `{ token, newPassword }`.
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
PROTOBASE_SMTP_URL=smtps://user:password@smtp.example.com:465 # or smtp://...:587, STARTTLS when the server offers it
|
|
115
|
+
PROTOBASE_MAIL_FROM=noreply@admin.example.com
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
`createAuth` reads both from the environment; the platform passes them, like `DATABASE_URL`. Set both or neither: one without the other, a URL that is not `smtp://` or `smtps://`, or a sender that is not a plain address stops `createAuth` with an error. Without them reset is off: `GET /api/auth/status` says `"passwordReset": false`, the page shows no link, and a reset request answers `400` (`RESET_PASSWORD_DISABLED`). A project can send the mail itself instead, or turn reset off whatever the environment says:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
createAuth({ ..., mailer: { send: async ({ to, subject, text }) => { /* deliver it */ } } })
|
|
122
|
+
createAuth({ ..., mailer: false })
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
[Jobs](jobs.md#mail)) send their own mail through the same server, with `mail` in their context, and so do the app's [email templates](email.md)).
|
|
126
|
+
|
|
127
|
+
- The email names the app and gives the link, as a button and in its plain-text part, that it works for 1 hour and once, and that it can be ignored (see [Protobase's own mail](email.md#protobases-own-mail))).
|
|
128
|
+
- The SMTP client greets the server with the sender's domain (`admin.example.com` above), not the container's `[127.0.0.1]`; a `?name=` on `PROTOBASE_SMTP_URL` overrides it.
|
|
129
|
+
- A request answers the same whether or not the address has an account, and the mail goes out after the answer, so neither the response nor its timing tells. A send that fails is logged by Better Auth.
|
|
130
|
+
- Requests are limited to 3 per minute and client. Setting a new password ends all of the user's sessions; the new password needs 12 characters like any other.
|
|
131
|
+
- `redirectTo` must be on a trusted origin (`BETTER_AUTH_URL`, one of the app's [other origins](#several-origins) or `TRUSTED_ORIGINS`); the admin app sends its own page with a `password-reset` marker, so the link opens the set-password page there.
|
|
132
|
+
- The link is on the origin the reset was asked on, when that is one of the app's.
|
|
133
|
+
- While the [sign-in policy](#sign-in-policy) turns passwords off, reset is off too (`403`).
|
|
134
|
+
|
|
135
|
+
## Emailed sign-in codes
|
|
136
|
+
|
|
137
|
+
With mail configured, the sign-in page offers "Email me a sign-in code": the person enters their address, gets a 6-digit code, and signs in with it instead of a password. It is Better Auth's [email OTP](https://www.better-auth.com/docs/plugins/email-otp) plugin: `POST /api/auth/email-otp/send-verification-otp` with `{ email, type: "sign-in" }`, then `POST /api/auth/sign-in/email-otp` with `{ email, otp }`.
|
|
138
|
+
|
|
139
|
+
- A code works for 5 minutes, once, and three wrong tries end it; asking again sends a new one and the old one stops working. Codes are stored hashed.
|
|
140
|
+
- It never creates an account: an unknown address gets no mail, and the answer is the same either way.
|
|
141
|
+
- Only sign-in codes are mailed. The plugin's own email verification, password reset and address change endpoints are off (`404`), and asking for another type of code answers `400`.
|
|
142
|
+
- An account with [two-factor authentication](#two-factor-authentication) is asked for its second step after the code too, as after a password.
|
|
143
|
+
- An account created by Protobase 0.3 or earlier, which has a password, is marked verified the first time it uses a code. Otherwise Better Auth would treat the code as taking over an unverified account and delete its password.
|
|
144
|
+
|
|
145
|
+
## Open sign-up
|
|
146
|
+
|
|
147
|
+
Off by default: accounts come from the host, an admin, an [invitation](#users-page) to the app or to an [organization](#organizations), or a [sign-in provider](#sign-in-with-github). An app where anyone may make an account, such as a public portal whose visitors save searches, turns it on with the role new accounts get:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
createAuth({ ..., roles: ['admin', 'member'], signUp: { role: 'member' } })
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The sign-in page then offers "Create an account", and so do [sign-in prompts](ui-config.md#sign-in-prompts)): the person enters their address, gets a 6-digit code, and enters it on a page of its own. Only once the code is right does the next page ask for their name and a password. The account is made only then, with the address verified, and signed in.
|
|
154
|
+
|
|
155
|
+
- `role` is one of `roles`, and never `admin`: `createAuth` stops with an error otherwise. Give it the least a stranger may hold; a request that names another role, or sets `role` through Better Auth's own endpoints, does not get it.
|
|
156
|
+
- It needs mail: without a mailer it is not offered, `GET /api/auth/status` has no `signUp`, and its endpoints answer `404`. Not with [organizations](#organizations), whose people join by invitation.
|
|
157
|
+
- `POST /api/auth/sign-up/send-code` with `{ email }` mails the code. It answers the same whether the address has an account or not; an address that has one is mailed a note saying so instead of a code, with a link to the sign-in page, so nobody can tell from the answer, and no second account is made.
|
|
158
|
+
- `POST /api/auth/sign-up/verify-code` with `{ email, code }` checks the code on its own and answers `{ ticket }`. A code works for 5 minutes, once, and three wrong tries end it; asking again sends a new one. Codes are stored hashed.
|
|
159
|
+
- `POST /api/auth/sign-up/create` with `{ ticket, name, password? }` makes the account for the address whose code was right, and signs in. A ticket works for 30 minutes, once, and is stored hashed; a password the policy refuses leaves it for another try. A used or expired one answers `SIGN_UP_TICKET_INVALID`, and the sign-up starts again with the address.
|
|
160
|
+
- The password follows the [sign-in policy](#sign-in-policy) as for an invitation: none while passwords are off, optional while emailed codes are on, 12 characters or more. What the policy requires, such as two-factor authentication, is set up after the first sign-in, as for every account.
|
|
161
|
+
- The three requests are limited per minute and client like sign-in (`signInPerMinute`, default 5), and refused (`NO_ADMIN_YET`) while the store has no user.
|
|
162
|
+
- `onUserCreated` runs for every account made this way.
|
|
163
|
+
|
|
164
|
+
## Passkeys
|
|
165
|
+
|
|
166
|
+
People add passkeys (Face ID, Touch ID, Windows Hello, a phone or a security key) on their account page, and sign in with "Sign in with a passkey". It is Better Auth's [passkey](https://www.better-auth.com/docs/plugins/passkey) plugin from `@better-auth/passkey`, under `/api/auth/passkey/*`.
|
|
167
|
+
|
|
168
|
+
- A passkey is made for the app's passkey domain, its RP ID: the host of `BETTER_AUTH_URL` (`admin.example.com`), or the host of one of its [other origins](#several-origins) set with `passkeyDomain` or `PROTOBASE_PASSKEY_DOMAIN`. For local development, `http://localhost` works.
|
|
169
|
+
- Each passkey keeps the RP ID it was made for (`passkey.rp_id`), so changing the passkey domain breaks none. Passkeys made before Protobase kept it count for the host of `BETTER_AUTH_URL`, which they were made for; renaming that host breaks them. The column is new in 0.10: the server refuses to start until the [migration](#migrations) that adds it is applied.
|
|
170
|
+
- On an app with several origins, every passkey works on each of them through [Related Origin Requests](https://passkeys.dev/docs/advanced/related-origins/) (Chrome and Edge 128, Safari 18, Firefox 152; at most 5 registrable domains).
|
|
171
|
+
- A browser asks for the passkeys of one RP ID at a time. When people have passkeys for more than one of the app's hosts, `GET /api/auth/status` lists them as `passkeyDomains`, the passkey domain first, and the sign-in page adds "Use a passkey made for {host}" for each of the others. `GET /api/auth/passkey/generate-authenticate-options?rpId=<host>` asks for one of them.
|
|
172
|
+
- A passkey sign-in asks for no second step: the passkey is something you have and something you are or know.
|
|
173
|
+
- Adding a passkey needs a session started in the last day; after that, the page asks to sign in again first.
|
|
174
|
+
- Passkeys can be renamed and removed on the account page; removing one here leaves it on the device.
|
|
175
|
+
|
|
176
|
+
## Two-factor authentication
|
|
177
|
+
|
|
178
|
+
On their account page, people turn on a second step after a password or an emailed sign-in code, with Better Auth's [two-factor](https://www.better-auth.com/docs/plugins/2fa) plugin:
|
|
179
|
+
|
|
180
|
+
- **Authenticator app:** the page shows a QR code (and the key, to type in), the person confirms it with a code from the app, and gets ten backup codes that each work once instead of a code. New backup codes replace the old ones.
|
|
181
|
+
- **Emailed code:** with mail configured, a 6-digit code mailed at each sign-in, valid for 5 minutes. An account with an app can also choose an emailed code at sign-in. Backup codes come with the app only.
|
|
182
|
+
|
|
183
|
+
Turning it on, off, or making new backup codes asks for the account's password when it has one. At sign-in, a correct password or code answers `{ "twoFactorRedirect": true, "twoFactorMethods": ["totp", "otp"] }` instead of a session, and `POST /api/auth/two-factor/verify-totp`, `verify-otp` or `verify-backup-code` with `{ code, trustDevice? }` finishes it; `trustDevice` skips the step in that browser for 30 days. Ten wrong codes in a row lock the account's second step for 15 minutes.
|
|
184
|
+
|
|
185
|
+
Sign-in with GitHub or another [provider](#sign-in-with-github) is not asked for the second step.
|
|
186
|
+
|
|
187
|
+
## Sign-in policy
|
|
188
|
+
|
|
189
|
+
Admins set how people sign in on the app's **Sign-in policy** page (the profile menu, under the user). Each method takes a rule:
|
|
190
|
+
|
|
191
|
+
| Method | Rules | Default |
|
|
192
|
+
| --- | --- | --- |
|
|
193
|
+
| Password | On, Off | On |
|
|
194
|
+
| Emailed sign-in code | On, Off | On |
|
|
195
|
+
| Passkeys | Optional, Required, Off | Optional |
|
|
196
|
+
| Two-factor authentication | Optional, Required, Off | Optional |
|
|
197
|
+
| Staff sign-in as a person | On, Email the person, Off | On |
|
|
198
|
+
| Sign in with ... through the platform | On, Off | On |
|
|
199
|
+
|
|
200
|
+
The server stores the policy in the `sign_in_policy` table, one row per save (the newest applies, the others are its history, with who saved each and when), and applies it to Better Auth's endpoints; the pages only show what it allows.
|
|
201
|
+
|
|
202
|
+
- **Off:** the server refuses the method with `403` (`SIGN_IN_METHOD_FORBIDDEN`) and the sign-in page leaves it out. Passkeys off also stops adding them; two-factor off stops turning it on, while those who have it keep being asked until they turn it off.
|
|
203
|
+
- **Required:** someone who signs in without it is sent to set it up first. The server holds back API tokens until then (`GET /api/auth/token` answers `403`, `SIGN_IN_SETUP_REQUIRED`), and refuses to turn two-factor authentication off or to remove the last passkey.
|
|
204
|
+
- Signed-in people keep their session after a change; a newly required method is asked for at their next token refresh, within 15 minutes.
|
|
205
|
+
- **Staff sign-in as a person** shows only with an [operator provider](#operator-provider). Off refuses [staff sign-in](#staff-sign-in) (`403`, `SIGN_IN_METHOD_FORBIDDEN`); Email the person mails the person each time, and needs mail: without it the server refuses to save it, and a saved one turns staff sign-in off until mail is back.
|
|
206
|
+
- **Sign in with ... through the platform** shows only with a [platform sign-in provider](#platform-sign-in-provider). Off refuses starting, finishing and connecting it (`403`, `SIGN_IN_METHOD_FORBIDDEN`) and the sign-in page leaves it out. It applies as Off while passkeys or two-factor authentication are Required: a provider's sign-in skips the app's second step, and the provider's own cannot be checked.
|
|
207
|
+
|
|
208
|
+
The server refuses a policy (`400`, `SIGN_IN_POLICY_REFUSED`, with the reason) that would lock people out: passwords and emailed codes both off (someone without a passkey could not sign in), passwords off without mail, two-factor authentication required with passwords off (turning it on asks for the password), or a policy the saving admin could not sign in with. Emailed codes need mail: without `PROTOBASE_SMTP_URL` they are off whatever the policy says, and so that a policy saved with mail cannot lock everyone out once mail goes away, password sign-in is then on as well.
|
|
209
|
+
|
|
210
|
+
The endpoints, for admins: `GET /api/auth/policy/sign-in` answers `{ policy, effective, mail, operator?, platformSignIn?, savedAt?, savedBy? }` (`effective` is the policy as it applies now, `operator` the operator provider's name, `platformSignIn` the platform sign-in provider's), and `POST /api/auth/policy/sign-in` with `{ password, emailCode, passkey, twoFactor, staffAccess, platformSignIn }` saves one. `GET /api/auth/account/sign-in-methods` tells the signed-in user the policy, what their account has (`password`, `passkeys`, `twoFactor`, `authenticatorApp`) and what it still has to set up (`missing`).
|
|
211
|
+
|
|
212
|
+
Sign-in providers the project configures in code, such as its own GitHub app, are not covered by the policy; the one the platform adds is. Neither are roles: the policy is the same for everyone.
|
|
213
|
+
|
|
214
|
+
## Staff sign-in
|
|
215
|
+
|
|
216
|
+
The team that runs an app (the operator: Protobase Cloud, or whoever hosts it) can sign in as one of its people to help them, without their password and without the app's keys leaving the app. Staff sign in with the operator's own identity provider, and the app trusts that provider for this alone: it never creates an account for them.
|
|
217
|
+
|
|
218
|
+
1. Staff open the app with `?staff-sign-in`, for example `https://admin.example.com/?staff-sign-in&email=sanne@example.com&reason=Ticket+4211`, from a support tool or by hand. The page asks for the person's address and a reason, at least 10 characters.
|
|
219
|
+
2. "Continue with ..." sends them to the operator provider, where they sign in.
|
|
220
|
+
3. The provider sends them back to the app, signed in as the person, with a banner across the top: who is signed in as whom, why, until when, and "Stop staff session". A refusal comes back to the same page with the reason.
|
|
221
|
+
|
|
222
|
+
The guardrails:
|
|
223
|
+
|
|
224
|
+
- **Permission:** the ID token's `groups` claim must hold the configured group (default `protobase-staff-access`).
|
|
225
|
+
- **A strong, recent sign-in:** the provider must report a passkey or a second step (`amr` with `mfa`, `hwk` or `swk`, or an `acr` from `acrValues`), at most 15 minutes old (`auth_time`; the app asks with `max_age`). The person's own two-factor and passkey requirements do not apply to staff.
|
|
226
|
+
- **A reason**, kept in the log with who signed in as whom.
|
|
227
|
+
- **Short and stoppable:** the session lasts 30 minutes (`sessionMinutes`, at most 240), is never extended, ends with the browser, and "Stop staff session" (`POST /api/auth/staff/stop`) ends it and signs the browser out. An API token it got before keeps working until it expires, at most 15 minutes later, like any other.
|
|
228
|
+
- **No takeover:** a staff session cannot change the person's password, email, passkeys, two-factor authentication or sessions, nor save the sign-in policy (`403`, `STAFF_CANNOT_CHANGE_SIGN_IN`).
|
|
229
|
+
- **The app decides:** admins turn staff sign-in off, or have the person emailed each time, with the [sign-in policy](#sign-in-policy).
|
|
230
|
+
- **One way in:** Better Auth's own `/admin/impersonate-user` and `/admin/stop-impersonating` are off (`404`), so app admins cannot sign in as their people.
|
|
231
|
+
- The start is limited to 5 per minute and client; only the browser that started a sign-in can finish it, within 10 minutes and once; and the page it comes back to must be on a trusted origin.
|
|
232
|
+
|
|
233
|
+
Admins find the log under the policy on the **Sign-in policy** page: each staff sign-in with the staff member, the person, the reason, and when it started and ended. It is the `staff_sign_in` table, and `GET /api/auth/staff/sign-ins` (admins) answers `{ signIns }`, newest first. A staff session is a session of Better Auth's admin plugin with `impersonatedBy` set to the staff member's address; `GET /api/auth/staff/session` answers `{ staff }` for it (`null` otherwise), which the banner shows.
|
|
234
|
+
|
|
235
|
+
### Operator provider
|
|
236
|
+
|
|
237
|
+
Any OpenID Connect provider works. Register the app with it as a confidential client:
|
|
238
|
+
|
|
239
|
+
- Redirect URI: `<BETTER_AUTH_URL>/api/auth/staff/callback`, and `<origin>/api/auth/staff/callback` for each of the app's [other origins](#several-origins): staff come back to the one they started on.
|
|
240
|
+
- The client authenticates with its secret (`client_secret_basic`) and PKCE (`S256`); scopes `openid email profile`.
|
|
241
|
+
- The ID token carries `groups`, `amr` (or `acr`) and `auth_time`, signed with a key the provider publishes in its JWKS.
|
|
242
|
+
|
|
243
|
+
On a platform, pass it in the environment:
|
|
244
|
+
|
|
245
|
+
```sh
|
|
246
|
+
PROTOBASE_OPERATOR_ISSUER=https://id.operator.example # its discovery is <issuer>/.well-known/openid-configuration
|
|
247
|
+
PROTOBASE_OPERATOR_CLIENT_ID=admin-example-com
|
|
248
|
+
PROTOBASE_OPERATOR_CLIENT_SECRET=...
|
|
249
|
+
PROTOBASE_OPERATOR_NAME="Protobase Cloud" # shown as "Continue with Protobase Cloud"; default "the operator"
|
|
250
|
+
PROTOBASE_OPERATOR_GROUP=protobase-staff-access # the default
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Set the first three together or none: some but not all of them stops `createAuth` with an error. In code, `operator` takes the same settings and a few more, and `false` turns staff sign-in off whatever the environment says:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
createAuth({ ..., operator: { issuer, clientId, clientSecret, name: 'Support', group: 'support-leads', scopes: ['groups'], acrValues: ['gold'], sessionMinutes: 15 } })
|
|
257
|
+
createAuth({ ..., operator: false })
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
`scopes` adds scopes for a provider that puts `groups` in the ID token only for a scope of its own (Okta's `groups`, for example), and `acrValues` names the levels that count as a strong sign-in for a provider that reports `acr` instead of `amr` (Keycloak's levels of assurance, for example); the app then asks for them with `acr_values`. `GET /api/auth/status` names the provider as `staffSignIn` while staff can sign in.
|
|
261
|
+
|
|
262
|
+
## Sign-in with GitHub
|
|
263
|
+
|
|
264
|
+
Projects have no sign-in provider of their own by default (a platform can add [one](#platform-sign-in-provider)). `socialProviders` adds Better Auth's [social providers](https://www.better-auth.com/docs/authentication/github), and the sign-in page shows "Continue with GitHub" when `github` is one:
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
createAuth({ ..., socialProviders: { github: { clientId: process.env.GITHUB_CLIENT_ID!, clientSecret: process.env.GITHUB_CLIENT_SECRET! } } })
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
- The callback URL to register with GitHub is `<BETTER_AUTH_URL>/api/auth/callback/github`, and the same path on each of the app's [other origins](#several-origins) it should sign in on. A GitHub OAuth App takes one callback URL, so it covers one origin; a GitHub App takes several. A GitHub App needs the Email addresses (read-only) account permission, or sign-in fails with `email_not_found`.
|
|
271
|
+
- A provider is also a public sign-up: someone without an account gets one with the [default role](#roles). Without a default role the sign-up is refused.
|
|
272
|
+
- `GET /api/auth/status` lists the provider ids as `socialProviders`.
|
|
273
|
+
- `encryptOAuthTokens: true` stores the provider's access, refresh and ID tokens encrypted with the secret.
|
|
274
|
+
- `onUserCreated: async (user, ctx) => { ... }` runs after any user is created, a social sign-up included (Better Auth's `databaseHooks.user.create.after`; the type is `UserCreatedHook`).
|
|
275
|
+
|
|
276
|
+
### Sign-in links and connected accounts
|
|
277
|
+
|
|
278
|
+
These work for every provider, the project's own and the [platform's](#platform-sign-in-provider):
|
|
279
|
+
|
|
280
|
+
- **Sign-in links:** `https://<app>/?sign-in=github` starts signing in with `github` at once, showing only "Signing in with GitHub" while the browser leaves, so someone the provider knows lands signed in without a sign-in page. A refusal comes back to the sign-in page with the reason (`?error=<code>`, for example `signup_disabled`), and does not start again. Someone signed in already just opens the app.
|
|
281
|
+
- **Connected accounts:** the **Sign-in & security** page lists the providers and offers "Connect GitHub" for one the account is not linked to. Connecting signs in at the provider and links that account whatever its address (`accountLinking.allowDifferentEmails`, which applies only to this explicit link by someone signed in). A provider account linked to another person already is refused (`account_already_linked_to_different_user`); a [staff session](#staff-sign-in) cannot link.
|
|
282
|
+
- **Linked up front:** `protobase users create <email> --github-id <id>`, or `createUser(auth, { ..., accounts: [{ providerId: 'github', accountId }] })`, links a new user to a GitHub user id, so GitHub finds them by it whatever address it reports.
|
|
283
|
+
- Without a link, a provider finds someone by address only when it vouches for the address (`email_verified`) and the account's address is verified; accounts created by an admin or on the host are.
|
|
284
|
+
|
|
285
|
+
## Sign-in with SAML
|
|
286
|
+
|
|
287
|
+
`sso.saml` adds the app's SAML 2.0 identity providers, such as Okta, Entra ID or Google Workspace, through Better Auth's [SSO plugin](https://www.better-auth.com/docs/plugins/sso). The sign-in page shows "Continue with {name}" for each:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
import { readFileSync } from 'node:fs'
|
|
291
|
+
|
|
292
|
+
createAuth({ ..., sso: { saml: [{ id: 'okta', name: 'Okta', domains: ['example.com'], idp: { metadata: readFileSync('auth/okta-metadata.xml', 'utf8') } }] } })
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
Instead of the metadata XML, `idp` takes the provider's entity ID, sign-in URL and signing certificate: `{ entityId, ssoUrl, certificate }`, where `certificate` is a PEM, or a list of them while one rotates. The id is lowercase letters, digits or `-`, and differs from those of the other sign-in providers; `name` defaults to `SSO`.
|
|
296
|
+
|
|
297
|
+
- **What the provider's admin needs:** the entity ID and metadata URL `<BETTER_AUTH_URL>/api/auth/sso/saml2/sp/metadata?providerId=okta`, which most providers import, and the ACS URL `<origin>/api/auth/sso/saml2/sp/acs/okta` for each of the app's [origins](#several-origins). The assertion carries the address as attribute `email` (or the NameID), and the name as `givenName` and `surname` or as `displayName`; `attributes: { email, name, firstName, lastName }` names other attributes. The NameID identifies the account, so a stable one is best: persistent, or the address.
|
|
298
|
+
- **Who gets in:** someone at one of `domains` (subdomains count) signs in to the account with that address, linked on the first sign-in, or gets a new one with the [default role](#roles), as with GitHub; without a default role the sign-up is refused. An address outside `domains` is refused, whether or not an account has it (`EMAIL_DOMAIN_NOT_ALLOWED`, or `account_not_linked` for an existing account). With [organizations](#organizations), people still join one by invitation.
|
|
299
|
+
- **Sign-in links** work as for the other providers: `https://<app>/?sign-in=okta` starts it at once. A SAML provider links an account by signing in, so the Sign-in & security page does not offer to connect it.
|
|
300
|
+
- `GET /api/auth/status` lists the providers as `samlProviders: [{ id, name }]`. Like `socialProviders`, they have no row in the [sign-in policy](#sign-in-policy), and a required passkey or two-factor authentication still holds back the token until it is set up.
|
|
301
|
+
- **No migration:** the providers live in config, so the store gets no table for them.
|
|
302
|
+
- The plugin's defaults stay: the response must answer a request this app made, within 5 minutes of clock skew, once. Not supported yet: SCIM, single logout, sign-in started at the provider, providers kept per organization, and signed or encrypted requests.
|
|
303
|
+
|
|
304
|
+
## Platform sign-in provider
|
|
305
|
+
|
|
306
|
+
A platform that hosts the app can add a sign-in provider for the people who have an account in it: any OpenID Connect provider, passed in the environment. Protobase Cloud passes its own, so people sign in with GitHub through the Protobase GitHub App; a self-hosted app passes [Dex, Keycloak](#self-hosting-with-dex-or-keycloak) or the like.
|
|
307
|
+
|
|
308
|
+
```sh
|
|
309
|
+
PROTOBASE_SIGN_IN_ISSUER=https://auth.example.com # its discovery is <issuer>/.well-known/openid-configuration
|
|
310
|
+
PROTOBASE_SIGN_IN_CLIENT_ID=erp-example-com
|
|
311
|
+
PROTOBASE_SIGN_IN_CLIENT_SECRET=...
|
|
312
|
+
PROTOBASE_SIGN_IN_PROVIDER=oidc # the default; the button, the callback and the namespace of the links
|
|
313
|
+
PROTOBASE_SIGN_IN_NAME="Acme SSO" # shown as "Continue with Acme SSO"; default GitHub for github, SSO otherwise
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
Register the app with the provider as a confidential client with the redirect URI `<BETTER_AUTH_URL>/api/auth/callback/<provider>` (`/api/auth/callback/oidc` by default), and the same path on each of the app's [other origins](#several-origins). The app authenticates with its secret (`client_secret_basic`) and PKCE (`S256`), asks for `openid email profile`, and takes the person from the ID token only, verified against the provider's JWKS, the issuer, the client and the nonce. The provider needs no userinfo endpoint.
|
|
317
|
+
|
|
318
|
+
- **No sign-up:** someone without an account is refused (`signup_disabled`); admins add people as before. A provider the project did not choose never opens the app to new people.
|
|
319
|
+
- **The project's own provider wins:** with `socialProviders.github` in code and `PROTOBASE_SIGN_IN_PROVIDER=github`, the project's GitHub app handles the sign-in and the platform's is left out.
|
|
320
|
+
- **The provider id:** set `PROTOBASE_SIGN_IN_PROVIDER=github` only when the provider's `sub` is the GitHub user id, as Protobase Cloud's is. Its links are then those of Better Auth's own GitHub provider, so an app that later brings its own GitHub app keeps every person's link. Dex's and Keycloak's `sub` is their own id, so keep the default `oidc` for them.
|
|
321
|
+
- **Startup:** the provider's settings are read when the app starts; when they cannot be, the provider is left out until the next start (the status and sign-in page do not offer it) and every other way to sign in works.
|
|
322
|
+
- The [sign-in policy](#sign-in-policy) has a row for it, and `GET /api/auth/status` names it as `platformSignIn: { provider, name }` besides listing it in `socialProviders`.
|
|
323
|
+
|
|
324
|
+
Set the first three together or none: some but not all of them stops `createAuth` with an error. In code, `signInProvider` takes the same settings and `scopes`, and `false` turns it off whatever the environment says:
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
createAuth({ ..., signInProvider: { issuer, clientId, clientSecret, provider: 'oidc', name: 'Acme SSO', scopes: ['groups'] } })
|
|
328
|
+
createAuth({ ..., signInProvider: false })
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Self-hosting with Dex or Keycloak
|
|
332
|
+
|
|
333
|
+
[Dex](https://dexidp.io) with its GitHub connector signs people in with GitHub through one GitHub OAuth app for every app, the way Protobase Cloud does:
|
|
334
|
+
|
|
335
|
+
```yaml
|
|
336
|
+
issuer: https://dex.example.com
|
|
337
|
+
connectors:
|
|
338
|
+
- type: github
|
|
339
|
+
id: github
|
|
340
|
+
name: GitHub
|
|
341
|
+
config: { clientID: $GITHUB_CLIENT_ID, clientSecret: $GITHUB_CLIENT_SECRET, redirectURI: https://dex.example.com/callback }
|
|
342
|
+
staticClients:
|
|
343
|
+
- id: erp-example-com
|
|
344
|
+
secret: $ERP_CLIENT_SECRET
|
|
345
|
+
name: ERP
|
|
346
|
+
redirectURIs: ['https://erp.example.com/api/auth/callback/oidc']
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
```sh
|
|
350
|
+
PROTOBASE_SIGN_IN_ISSUER=https://dex.example.com PROTOBASE_SIGN_IN_CLIENT_ID=erp-example-com PROTOBASE_SIGN_IN_CLIENT_SECRET=$ERP_CLIENT_SECRET PROTOBASE_SIGN_IN_NAME=GitHub
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
[Keycloak](https://www.keycloak.org): in a realm, add GitHub (or any other provider) under **Identity providers**, with **Trust email** on so Keycloak vouches for the GitHub address. Create a client with **Client authentication** on, the **Standard flow** and the valid redirect URI `https://erp.example.com/api/auth/callback/oidc`, and take its secret from **Credentials**:
|
|
354
|
+
|
|
355
|
+
```sh
|
|
356
|
+
PROTOBASE_SIGN_IN_ISSUER=https://keycloak.example.com/realms/acme PROTOBASE_SIGN_IN_CLIENT_ID=erp PROTOBASE_SIGN_IN_CLIENT_SECRET=... PROTOBASE_SIGN_IN_NAME="Acme SSO"
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
People sign in with the address their account here has, or connect the provider on the Sign-in & security page once.
|
|
360
|
+
|
|
361
|
+
## Roles
|
|
362
|
+
|
|
363
|
+
The roles are a project setting: `createAuth({ ..., roles: ['admin', 'auditor', 'sales', 'accountant'] })`. `admin` is always included and first; the default is `['admin', 'user']`. Names are lowercase letters, digits, `_` and `-`. `defaultRole` is what a new user gets when no role is given. Without it, and with more than one role besides `admin`, creating a user without a role is an error that lists the choices (the CLI then needs `--role`); with exactly one other role, that role is the default. `auth.defaultRole` is `undefined` when a role must be chosen. A user can hold several roles (`setUserRole(auth, { email, role: 'sales,accountant' })`), and acts with one of them at a time, the [active role](#active-role).
|
|
364
|
+
|
|
365
|
+
`createUser`, `setUserRole` and Better Auth's own admin endpoints refuse a role outside the list. `roleChoices(auth)` returns the list, for the CLI's `users set-role` and for UIs. Only `admin` gains Better Auth admin-plugin permissions (managing users); every other role is for your access rules, which see them as `ctx.user.roles`. Besides `admin`, the role `ai` opens the [assistant](assistant.md#who-sees-it)) when the project lists it.
|
|
366
|
+
|
|
367
|
+
### Active role
|
|
368
|
+
|
|
369
|
+
A session acts with one role, as it works in one [organization](#organizations): its token carries that role alone, so the API, `/meta` and with it the views, the sidebar and what the app offers follow it, as do the admin-only endpoints (the sign-in policy, the log of staff sign-ins, Better Auth's user management). A new session starts with the role the person last chose, else their first. `POST /api/auth/role/switch` with `{ role }`, one the person holds, moves the session to it; the next token carries it, without signing in again, and the user keeps it (`user.last_role`) for their next session. `GET /api/auth/role/current` answers `{ active, roles }`, the active role and the roles held, each with its label, where the session works. Someone holding one role has nothing to switch.
|
|
370
|
+
|
|
371
|
+
The app offers the roles in the profile menu, below the organizations, for someone holding more than one. The client has them as `session.roles` (`current`, `switchTo`). With organizations, the roles held are the global ones and those of the membership in the organization the session works in; a role not held there leaves the session with the person's first role there. Managing organizations (creating, entering, inviting and giving app roles) follows every role held. The session keeps its role in `session.active_role`; [`protobase auth migration`](#migrations) writes both columns.
|
|
372
|
+
|
|
373
|
+
## Organizations
|
|
374
|
+
|
|
375
|
+
Off by default: without `organizations`, an app is one organization, as above. Turned on, people work in one organization at a time, `.tenant` resources show only its rows, and the organization comes from the signed-in person instead of `tenant`:
|
|
376
|
+
|
|
377
|
+
```ts
|
|
378
|
+
const auth = createAuth({
|
|
379
|
+
...,
|
|
380
|
+
roles: {
|
|
381
|
+
admin: 'Superuser',
|
|
382
|
+
manager: { label: 'Manager', grants: ['sales', 'accountant'] },
|
|
383
|
+
sales: 'Sales',
|
|
384
|
+
accountant: 'Accountant',
|
|
385
|
+
},
|
|
386
|
+
organizations: { create: 'everyone' },
|
|
387
|
+
})
|
|
388
|
+
const authenticate = betterAuthAuthenticator({ auth })
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
It is Better Auth's organization plugin, with its tables named in snake_case (`organization`, `member`, `invitation`, `session.active_organization_id`); `protobase auth migration` writes them, and `user.last_organization_id`, as the project's next migration.
|
|
392
|
+
|
|
393
|
+
**Two kinds of role.** Every membership has an organization role, Better Auth's `owner`, `admin` or `member`, which decides who manages the organization: owners and admins invite, remove and change members and edit its details; only owners hand it over, delete it or make owners. App roles, the project's `roles`, decide what the app shows and allows. A user holds them **globally** (the admin plugin's `role` column, as without organizations), which applies in every organization, or **per membership** (`member.app_roles`), which applies there only. A request's role, `ctx.user.roles`, is the session's [active role](#active-role), of either kind; access rules never see the organization role. `admin` is the superuser: global only, it holds every role. With organizations every role needs a label; `owner` and `member` cannot be app role names, and `user` stands for an account without a global role.
|
|
394
|
+
|
|
395
|
+
**Who gives which role.** Someone may give or take away a member's app role only if they hold it, in that organization or globally, or hold a role whose `grants` list it (`manager` above gives `accountant` without holding it); grants do not chain, and a superuser may give any. Only a superuser changes global roles. The creator of an organization becomes its owner with every app role (`creatorAppRoles`, default `'all'`), so they can give them on; for a role added later, list it in a role owners hold, or run `protobase organizations add-app-role <role> --to owners`.
|
|
396
|
+
|
|
397
|
+
**The organization in the token.** A new session starts in the organization the person last worked in, else their oldest membership. `/api/auth/token` adds `org` (the organization), `org_role` and `app_roles` (the membership's, narrowed to the active role), read when the token is issued, so a removed member or a changed role shows within its 15 minutes. `betterAuthAuthenticator` makes `org` the tenant and `ctx.organization.id`, and adds `app_roles` to the global roles. `POST /api/auth/organization/switch` with `{ organizationId }` moves the session; the next token names the new organization, without signing in again. Someone with a global role may switch into any organization, without being a member, and sees there what those roles allow; each such entry is an `organization.entered` [audit event](api.md#audit-events)). `GET /api/auth/organization/search?query=&offset=` lists every organization for them, ordered by slug, 20 at a time with the `total`, narrowed by text in the name or slug; the Organizations page shows it as All organizations. A list across organizations stays a resource without `.tenant`, readable by the global role.
|
|
398
|
+
|
|
399
|
+
**Invitations.** Better Auth's `POST /api/auth/organization/invite-member` with `{ organizationId, email, role, appRoles }`; inviting again replaces the earlier invitation, and it works for `invitationDays` (default 7). The email links to `{baseURL}/-/invitation?token=…` (`invitationUrl`), with a token signed with `secret`, since members can read invitation ids; without a mailer, the answer carries the `link` to pass on. The link page shows `GET /api/auth/invitation?token=`, accepts with `POST /api/auth/invitation/accept` for someone signed in as the invited address, and makes an account with `POST /api/auth/invitation/sign-up` (`{ token, name, password? }`) for an address without one. The link page and its sign-up serve [invitations to the app](#users-page) too. [Open sign-up](#open-sign-up) is not available with organizations: an invitation is the address someone vouched for.
|
|
400
|
+
|
|
401
|
+
**Members.** Better Auth's `list-members`, `update-member-role`, `remove-member` and `leave`; `POST /api/auth/organization/set-app-roles` (`{ memberId, appRoles }`); `POST /api/auth/organization/transfer-ownership` (`{ memberId, keepAs? }`) makes a member owner and the caller an admin in one step. `GET /api/auth/organization/current` answers the organization a session works in with the caller's roles there.
|
|
402
|
+
|
|
403
|
+
**Deleting** an organization (Better Auth's `delete`, owners only) is refused with `409 ORGANIZATION_HAS_RECORDS` while a `.tenant` resource has rows in it; `deleteRecords: 'cascade'` leaves it to the project's foreign keys. `beforeDelete` may refuse too.
|
|
404
|
+
|
|
405
|
+
**Ids.** `generateId` makes a new organization's id, by default a UUID; `.tenant` columns hold it, so `text` or `uuid`. An app whose tenant columns are integers takes the next value of its own organizations sequence instead, and adds its own row in `onCreated`, as the ERP example does (`examples/erp/auth/auth.ts`). An app that ran as one organization keeps its tenant value: `protobase organizations create "Acme" --owner you@example.com --id 1 --members all` makes that organization, with every user in it: superusers as owners, everyone else as a member, their global roles moved onto the membership.
|
|
406
|
+
|
|
407
|
+
The app's own pages are under the profile menu: the switcher, "Organizations" (theirs, a new one, and for global roles any one), "Organization" (details, leaving, handing over, deleting), and for owners and admins "Members" and "Invitations". The client has them as `session.organizations` (`list`, `current`, `switchTo`, `members`, `invite`, ...).
|
|
408
|
+
|
|
409
|
+
## Tokens and roles
|
|
410
|
+
|
|
411
|
+
Sign in at `POST /api/auth/sign-in/email` (or with a code or passkey), then `GET /api/auth/token` (with the session cookie) returns `{ token }`, an EdDSA JWT valid for 15 minutes, unless the [sign-in policy](#sign-in-policy) requires something the account has not set up; the keys are at `/api/auth/jwks`. Send it as `Authorization: Bearer <token>`. `/token` is the only way to a token in the browser: the `set-auth-jwt` header of Better Auth's JWT plugin is off.
|
|
412
|
+
|
|
413
|
+
`betterAuthAuthenticator` maps the claims to a session: `sub` is the user id, `roles` come from the `role` claim, the session's [active role](#active-role) of those in the admin plugin's `role` column on the user (a comma separated string; the first user is always `admin`). Access rules see them as `ctx.user.roles`. Without organizations, the tenant is the configured `tenant` option, never a claim; with them, it is the organization the session works in (`org`, see [organizations](#organizations)). The key set is cached in memory: read once, again every 10 minutes (so removed keys stop being accepted), and again when a token names an unknown `kid` (a rotated key), at most once per 30 seconds however many such tokens arrive (`cache: { refreshMs, minReloadMs }`). Pass `jwksUrl` and `issuer` instead of `auth` when the API runs apart from the auth server.
|
|
414
|
+
|
|
415
|
+
`jwtAuthenticator` accepts asymmetric signature algorithms only unless you pass `algorithms` explicitly (the tests do, for HS256), so a token cannot be forged with a public key used as an HMAC secret; Better Auth signs with its own key pair and publishes the public keys at `/api/auth/jwks`. An expired, tampered, wrongly signed or missing token is a `401` problem with `WWW-Authenticate: Bearer`; with an [anonymous role](access.md#anonymous-access)), a missing one on the resources API is served as that role instead.
|
|
416
|
+
|
|
417
|
+
## Users of another app
|
|
418
|
+
|
|
419
|
+
An API can admit the signed-in users of another Protobase app with the tokens that app issues, so that app's admin app can draw its resources as [remote resources](remote-resources.md)). `betterAuthAuthenticator`'s `apps` lists each such app by its `baseURL`, the issuer of its tokens, with the roles its users' global roles become here:
|
|
420
|
+
|
|
421
|
+
```ts
|
|
422
|
+
betterAuthAuthenticator({ auth, apps: [{ issuer: 'https://shop.example.com', roles: { admin: 'shop' }, tenant: 1 }] })
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
Their tokens are verified against that app's keys at `<issuer>/api/auth/jwks`; a role it does not map is dropped, a role held in one of its organizations never counts, and a user left with none gets `403`; their id here is `<sub>@<that app's host>`. See [Signing in at the source](remote-resources.md#signing-in-at-the-source)).
|
|
426
|
+
|
|
427
|
+
## Tokens for scripts and curl
|
|
428
|
+
|
|
429
|
+
There is no development login. For scripts, mint a token for an existing user on the host (the CLI exposes it as `protobase token you@example.com`):
|
|
430
|
+
|
|
431
|
+
```ts
|
|
432
|
+
import { issueToken } from '@protobase/server'
|
|
433
|
+
|
|
434
|
+
const { token, expiresAt } = await issueToken(auth, { email: 'you@example.com', ttlSeconds: 3600 }) // default 15 minutes, at most 24 hours
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
It is signed with the same keys and carries the same claims as a token from `/api/auth/token`, with the role the user last chose, else their first, so the API treats it as any other. Banned and unknown users get an error. There is no HTTP route for this.
|
|
438
|
+
|
|
439
|
+
## Several origins
|
|
440
|
+
|
|
441
|
+
An app can be served on more than one origin, such as its platform address and the customer's own domain. `origins` lists the others; it defaults to `PROTOBASE_ORIGINS`, which a platform sets:
|
|
442
|
+
|
|
443
|
+
```ts
|
|
444
|
+
createAuth({ ..., baseURL: 'https://erp-x1.protobase.net', origins: ['https://admin.shop.com'], passkeyDomain: 'admin.shop.com' })
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
- A request to one of them is answered for its own host: the sign-in callbacks, the [password reset](#password-reset) link and its `redirectTo`, and a [staff sign-in](#staff-sign-in) stay there. A request to any other host is answered for `baseURL`.
|
|
448
|
+
- Tokens keep `baseURL` as issuer and audience, whichever origin issued them, so the API takes them on every origin. `auth.issuer` is that value and `auth.origins` every origin, `baseURL`'s first.
|
|
449
|
+
- Sessions are cookies of each host: a person signs in once per origin.
|
|
450
|
+
- `GET /.well-known/webauthn` answers `{ "origins": [...] }` with every origin of the app, on each of them, so its [passkeys](#passkeys) work on all. `protobase serve` and `protobase dev` send that path to the server, whatever the bundle's `api` says; a host in front must too.
|
|
451
|
+
- Each origin is a URL with the scheme of `baseURL`; one that is not stops the server at startup. The host that serves the app must pass each origin's own `Host` header on.
|
|
452
|
+
- The app's own origins may call each other's API from the browser ([CORS](api.md#cors))).
|
|
453
|
+
|
|
454
|
+
## Public endpoints
|
|
455
|
+
|
|
456
|
+
Only the Better Auth routes under `/api/auth/*` (sign-in, password reset, [open sign-up](#open-sign-up), the start and callback of a [staff sign-in](#staff-sign-in) and the like), `GET /api/auth/status` and, with [several origins](#several-origins), `GET /.well-known/webauthn` are reachable without a token; the account, policy and staff session endpoints there need a session. The API (`/api/v1/*`) and the system endpoints (`/api/meta`, `/api/openapi.json`, `/api/docs`) all answer `401` without one, but for what an [anonymous role](access.md#anonymous-access)) reads in `/api/v1` and `/api/meta`, when the project defines one. [Public forms](forms.md#routes)) (`/api/forms/*`) need no token either.
|
|
457
|
+
|
|
458
|
+
## Roles and access in the API
|
|
459
|
+
|
|
460
|
+
Roles are bundles of capabilities (`defineRoles` in `@protobase/schema`: `orders.read.own`, `costs.read`, `*.read`, ...). Pass the result as `options.roles` to `createAdmin`, and every operation without an explicit rule needs the matching capability; a capability may be limited to [fields and rows](access.md#capabilities)). The token's `roles` become `ctx.user.roles` in rules; the user id (`sub`) is what `.own` compares with the resource's `.owner(...)` field, so it must be the id that field stores. How the API enforces them (hidden fields, row filters, record permissions) is in [the REST API reference](api.md#access)).
|
|
461
|
+
|
|
462
|
+
**User ids.** Better Auth gives a new user a random 32-character text id, so an `.owner(...)` field holds `text`: the server refuses to start (`protobase dev`, `serve`) when one holds integers or UUIDs, naming the field. An app whose owner columns are integers, such as a relation to its own users table, gives `createAuth` a `generateUserId` that takes the next value of that table's sequence, and adds the user's row there, as the ERP example does (`examples/erp/auth/auth.ts`, with the row added by `db/migrations/019_auth_users.sql` when someone joins an organization). A request by a user whose id the owner field cannot hold, such as one made before `generateUserId`, fails with an `owner-id-mismatch` problem that names the field.
|