@protobase/cli 0.47.1 → 0.49.0
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/build/config-bundle.js +6 -1
- package/dist/build/entry-code.js +30 -17
- package/dist/cli/package.js +1 -1
- package/dist/config/build-project.js +70 -0
- package/dist/config/called-config.js +11 -0
- package/dist/config/config-defaults.js +30 -0
- package/dist/config/env-values.js +20 -0
- package/dist/config/lazy-imports.js +18 -0
- package/dist/config/merge-config.js +72 -0
- package/dist/config/project-files.js +48 -0
- package/dist/config/project-parts.js +201 -0
- package/dist/config/read-config.js +31 -0
- package/dist/config/source-config.js +37 -0
- package/dist/dev/api-child.js +2 -1
- package/dist/dev/api-process.d.ts +1 -0
- package/dist/dev/api-process.js +5 -2
- package/dist/dev/auth-drift-lines.js +8 -0
- package/dist/dev/create-db.js +2 -2
- package/dist/dev/entry.d.ts +5 -0
- package/dist/dev/entry.js +17 -12
- package/dist/dev/project.js +23 -47
- package/dist/dev/run.js +3 -2
- package/dist/main.js +3 -3
- package/dist/migration/load-project.js +34 -34
- package/dist/migration/parts.js +2 -2
- package/dist/migration/summary.js +2 -2
- package/dist/mobile/api-origin.js +4 -4
- package/dist/mobile/load-project.js +13 -12
- package/dist/mobile/register.js +1 -1
- package/dist/mobile/run.js +1 -1
- package/dist/organizations/register.js +1 -1
- package/dist/project/error-reporter.js +3 -3
- package/dist/project/import-project-file.js +4 -2
- package/dist/project/load-bundle-project.js +16 -25
- package/dist/project/load-data-config.js +2 -2
- package/dist/project/pg-db.js +10 -4
- package/dist/project/placeholder-secret.js +2 -2
- package/dist/project/project-admin.js +12 -11
- package/dist/scaffold/plan.js +7 -3
- package/dist/scaffold/protobase-tables.js +8 -13
- package/dist/scaffold/run.js +1 -1
- package/dist/serve/create-admin.js +9 -4
- package/dist/serve/host-module-ids.js +1 -0
- package/dist/serve/host-modules.js +3 -1
- package/dist/serve/lifecycle.js +19 -14
- package/dist/serve/load-bundle.js +41 -11
- package/dist/serve/mobile-release.js +1 -1
- package/dist/serve/serve-bundle.js +3 -3
- package/dist/supabase/auth-config.js +33 -28
- package/dist/supabase/config-jobs.js +28 -27
- package/dist/supabase/config-object.js +25 -0
- package/dist/supabase/cron-job-file.js +13 -14
- package/dist/supabase/cron-jobs-write.js +6 -12
- package/dist/supabase/cron-plan.js +3 -51
- package/dist/supabase/project-files.js +4 -9
- package/dist/supabase/report.js +3 -3
- package/dist/users/load-auth.js +16 -15
- package/dist/worker/start-worker.js +18 -15
- package/package.json +8 -6
- package/skills/protobase/SKILL.md +2 -1
- package/skills/protobase/references/features/notifications.md +1 -1
- package/skills/protobase/references/features/sign-in-and-accounts.md +1 -0
- package/skills/protobase/references/features/start-a-project.md +3 -0
- package/skills/protobase/references/getting-started.md +2 -2
- package/skills/protobase/references/guides/api-functions.md +4 -4
- package/skills/protobase/references/guides/deploy.md +8 -8
- package/skills/protobase/references/guides/existing-database.md +11 -31
- package/skills/protobase/references/guides/first-plugin.md +1 -1
- package/skills/protobase/references/guides/mobile-app.md +5 -3
- package/skills/protobase/references/guides/plugin-build.md +1 -1
- package/skills/protobase/references/guides/push-notifications.md +6 -7
- package/skills/protobase/references/guides/remote-resources.md +22 -31
- package/skills/protobase/references/reference/access.md +3 -3
- package/skills/protobase/references/reference/ai.md +1 -9
- package/skills/protobase/references/reference/api.md +15 -15
- package/skills/protobase/references/reference/app-identity.md +9 -10
- package/skills/protobase/references/reference/assistant.md +6 -6
- package/skills/protobase/references/reference/auth.md +126 -96
- package/skills/protobase/references/reference/bottom-bar.md +1 -1
- package/skills/protobase/references/reference/cli.md +36 -86
- package/skills/protobase/references/reference/comments.md +0 -8
- package/skills/protobase/references/reference/config.md +152 -0
- package/skills/protobase/references/reference/data-config.md +2 -2
- package/skills/protobase/references/reference/email.md +36 -37
- package/skills/protobase/references/reference/events.md +1 -1
- package/skills/protobase/references/reference/files.md +18 -23
- package/skills/protobase/references/reference/forms.md +5 -17
- package/skills/protobase/references/reference/history.md +2 -3
- package/skills/protobase/references/reference/installable-app.md +1 -1
- package/skills/protobase/references/reference/jobs.md +53 -56
- package/skills/protobase/references/reference/languages.md +4 -2
- package/skills/protobase/references/reference/mcp.md +11 -11
- package/skills/protobase/references/reference/notifications.md +26 -24
- package/skills/protobase/references/reference/plugins.md +11 -14
- package/skills/protobase/references/reference/remote-resources.md +18 -24
- package/skills/protobase/references/reference/supabase-realtime.md +1 -1
- package/skills/protobase/references/reference/supabase-rest.md +1 -1
- package/skills/protobase/references/reference/supabase-storage.md +1 -1
- package/skills/protobase/references/reference/ui-config.md +15 -17
- package/skills/protobase/references/reference/versioning.md +1 -1
- package/skills/protobase/references/reference/webhooks.md +6 -7
|
@@ -4,88 +4,113 @@
|
|
|
4
4
|
|
|
5
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
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
|
|
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 Protobase; the SSO and MCP plugins load only when the config turns them on.
|
|
8
|
+
|
|
9
|
+
Sign-in is on by default. Its settings are the `auth` key of [`protobase.config.ts`](config.md), and it takes the app's address, secret and name from `url`, `secret` and `name`:
|
|
8
10
|
|
|
9
11
|
```ts
|
|
10
|
-
|
|
12
|
+
// protobase.config.ts
|
|
13
|
+
import { defineConfig } from '@protobase/config'
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
export default defineConfig({
|
|
16
|
+
auth: { roles: ['admin', 'sales'] },
|
|
17
|
+
authenticate: { tenant: 1 },
|
|
18
|
+
})
|
|
14
19
|
```
|
|
15
20
|
|
|
16
|
-
`
|
|
21
|
+
`auth: false` turns sign-in off. The app serves it at `/api/auth/*` next to the API at `/api/v1`. The snippets below show the keys of `auth` they set.
|
|
17
22
|
|
|
18
23
|
## Admin store
|
|
19
24
|
|
|
20
|
-
Users, sessions and signing keys stay apart from the application's tables
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
createAuth({ database: { dialect: new PostgresDialect({ pool }), type: 'postgres', schemaName: 'auth', transaction: true }, ... })
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
Build that pool with `appPool(pg, process.env.DATABASE_URL)` (from `@protobase/server`), as the presets do: it is the app's one pool on that database, which `protobase dev` and `serve` share, so the store opens no connections beyond [`PROTOBASE_DB_CONNECTIONS`](cli.md#serve), and a database restart is printed instead of stopping the app. A pool of the app's own, such as `new pg.Pool(...)`, opens its connections beside the runtime's.
|
|
25
|
+
Users, sessions and signing keys stay apart from the application's tables, in a schema of the app's own database, `auth` by default (`auth: { schema: 'accounts' }` names another), so a host that gives an application one database (and a role that cannot create databases) is enough. The store shares the app's one pool on `DATABASE_URL`, which `protobase dev` and `serve` open, so it opens no connections beyond [`PROTOBASE_DB_CONNECTIONS`](cli.md#serve), and a database restart is printed instead of stopping the app.
|
|
27
26
|
|
|
28
27
|
### Migrations
|
|
29
28
|
|
|
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.
|
|
29
|
+
The store's tables come from migrations that ship with Protobase, one version after another, and 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. The store records each version it applied in its `auth_migrations` table. When an upgrade of Protobase brings new versions, write them as the project's next migration:
|
|
31
30
|
|
|
32
31
|
```sh
|
|
33
32
|
bun run db:migrate # the database at the project's current schema
|
|
34
|
-
bun run protobase auth migration #
|
|
35
|
-
bun run db:migrate # applies
|
|
33
|
+
bun run protobase auth migration # reads auth_migrations; writes the versions not applied as db/migrations/NNN_auth.sql
|
|
34
|
+
bun run db:migrate # applies them
|
|
36
35
|
```
|
|
37
36
|
|
|
38
|
-
The file only adds tables, columns and indexes, never drops one
|
|
37
|
+
Each version's statements are followed by its row in `auth_migrations`. 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).
|
|
38
|
+
|
|
39
|
+
Every store has the tables of every feature, whether the app turns the feature on or not: [passkeys](#passkeys), [two-factor authentication](#two-factor-authentication), [organizations](#organizations) and the [OAuth provider for MCP](#oauth-for-mcp-clients). Turning a feature on or off needs no migration and drops nothing; the columns a feature adds to the user and session tables are empty until it is on. SAML providers and open sign-up keep nothing in the store.
|
|
39
40
|
|
|
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.
|
|
41
|
+
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. The names the app configures, the store's schema (`schemaName`), the [user table](#an-existing-users-table) and its columns, and uuid ids (`generateUserId: 'uuid'`), are the ones in effect when the file is written. Changing one later is a migration of the project's own, such as a rename.
|
|
41
42
|
|
|
42
|
-
The server never changes the schema. `protobase serve` and `protobase dev` read
|
|
43
|
+
The server never changes the schema, and never compares it with what Better Auth needs. `protobase serve` and `protobase dev` read `auth_migrations` at startup and refuse to start while a version of this Protobase is not applied, naming it and `protobase auth migration`, or while the store has a version newer than this Protobase knows. 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. Only [`protobase dev`](cli.md#dev), on the developer's machine, compares the store's tables with what the app's Better Auth needs, and warns at startup about each table, column or index that drifted, such as one changed by hand.
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
No resource may be over a table of the store, which holds password hashes, sessions, tokens and signing keys: `protobase serve` and `protobase dev` refuse to start with one, naming it, whatever the table is called, and [`protobase scaffold`](cli.md#scaffold) never writes one. The API, the Supabase-compatible endpoints, live updates and MCP read the app's resources only, so none of them reads the store.
|
|
45
46
|
|
|
46
|
-
|
|
47
|
+
A store made before 0.48, when `protobase auth migration` compared the database with Better Auth's tables, has no `auth_migrations`. For it, `protobase auth migration` writes the step that adopts it, once: it runs each version in a scratch schema, in a transaction it rolls back, to find the tables and columns that version makes, records each version whose tables and columns the store has, with their types, as applied, and writes the versions whose tables it has none of, such as MCP's for an app that never turned MCP on. The file checks again, when it is applied, that the store has every table and column it adopts, and stops otherwise, naming them. A store that has only some of a version's tables and columns, or has them with other types, is refused, naming what differs: bring it to the schema of Protobase 0.47 first, with that version's `protobase auth migration`. A store set up by Protobase 0.6, with Better Auth's camelCase names, or by `auth:migrate` before it, goes through 0.47 the same way.
|
|
47
48
|
|
|
48
49
|
### An existing users table
|
|
49
50
|
|
|
50
51
|
The store can use a users table another system made, such as Supabase's `auth.users`, so its rows keep signing in and every foreign key, trigger and function that names the table keeps working:
|
|
51
52
|
|
|
52
53
|
```ts
|
|
53
|
-
|
|
54
|
-
database: { dialect, type: 'postgres', schemaName: 'auth' },
|
|
54
|
+
auth: {
|
|
55
55
|
users: { table: 'users', columns: { role: 'app_role' }, metadata: 'raw_user_meta_data' },
|
|
56
56
|
passwordHashes: ['bcrypt'],
|
|
57
57
|
generateUserId: 'uuid',
|
|
58
|
-
|
|
59
|
-
})
|
|
58
|
+
}
|
|
60
59
|
```
|
|
61
60
|
|
|
62
61
|
- `users.table` names the table, default `user`. `users.columns` maps a column, by the name Protobase gives it (`name`, `email`, `email_verified`, `email_confirmed_at`, `image`, `created_at`, `updated_at`, `role`, `banned`, `ban_reason`, `ban_expires`, `two_factor_enabled`, `last_role`, `last_organization_id`), to the table's own; here the app's roles go to `app_role`, since Supabase's `role` holds the Postgres role. The other tables keep their names and point at this one.
|
|
63
|
-
- `users.metadata` names a JSON column for each user's metadata: `createUser(auth, { ..., metadata: { full_name: 'Carol' } })` writes it, so a trigger that reads `raw_user_meta_data->>'full_name'` sees it.
|
|
62
|
+
- `users.metadata` names a JSON column for each user's metadata, which the migrations add as `jsonb` when the table lacks it: `createUser(auth, { ..., metadata: { full_name: 'Carol' } })` writes it, so a trigger that reads `raw_user_meta_data->>'full_name'` sees it.
|
|
64
63
|
- `passwordHashes: ['bcrypt']` also verifies bcrypt hashes (`$2a$`, `$2b$`, `$2y$`), and stores the password again as Better Auth's own scrypt after its first sign-in. The 12-character minimum holds for new passwords only, so a shorter existing one still signs in.
|
|
65
64
|
- `generateUserId: 'uuid'` makes each new user's id a random UUID in the server, for a table whose `id` is a `uuid` without a default, and makes every id column of the other tables a `uuid` too.
|
|
66
65
|
|
|
67
|
-
The columns Better Auth needs that the table lacks come first, from a migration of the project's own; `protobase auth migration` then writes the other tables next to it, and another migration fills the columns from what the other system recorded and gives each password its `credential` row in `account`. For Supabase, [`protobase import supabase`](cli.md#import-supabase) writes all three and these options.
|
|
66
|
+
The store's migrations make the user table only when it is missing, and add each of its columns only when the table lacks it, so a table another system made keeps its own. A required column (`name`, `email`, `email_verified`) cannot be added to a table with rows, so the columns Better Auth needs that the table lacks come first, nullable, from a migration of the project's own; `protobase auth migration` then writes the other tables next to it, and another migration fills the columns from what the other system recorded and gives each password its `credential` row in `account`. For Supabase, [`protobase import supabase`](cli.md#import-supabase) writes all three and these options.
|
|
68
67
|
|
|
69
68
|
## Configuration
|
|
70
69
|
|
|
71
70
|
| Variable | |
|
|
72
71
|
| --- | --- |
|
|
73
|
-
| `
|
|
74
|
-
| `
|
|
75
|
-
| `
|
|
72
|
+
| `PROTOBASE_SECRET` | the app's secret, the default of `secret`: signs cookies and encrypts the token keys; `openssl rand -base64 32`; never committed (`.env.example` has a placeholder) |
|
|
73
|
+
| `PROTOBASE_URL` | the app's public URL, the default of `url` (in `protobase dev`, the dev server's own); issuer and audience of the tokens; when tunnelling, the https tunnel URL |
|
|
74
|
+
| `PROTOBASE_TRUSTED_ORIGINS` | extra comma separated origins, the default of `auth.trustedOrigins`; a dev tunnel needs its exact host here, such as `https://my-tunnel.trycloudflare.com`, since no wildcard is trusted |
|
|
76
75
|
| `PROTOBASE_ORIGINS` | the other origins the app is served on, comma separated, passed by the platform; see [Several origins](#several-origins) |
|
|
77
|
-
| `PROTOBASE_PASSKEY_DOMAIN` | the host new [passkeys](#passkeys) are made for, passed by the platform; default the host of `
|
|
76
|
+
| `PROTOBASE_PASSKEY_DOMAIN` | the host new [passkeys](#passkeys) are made for, passed by the platform; default the host of the app's `url` |
|
|
78
77
|
| `PROTOBASE_CORS_ORIGINS` | other websites whose browser code may call the API, `*` or comma separated http and https origins, passed by the platform; see [CORS](api.md#cors) |
|
|
79
78
|
| `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 |
|
|
80
|
-
| `PROTOBASE_APP_NAME` | the app's name, passed by the platform: it overrides `
|
|
79
|
+
| `PROTOBASE_APP_NAME` | the app's name, passed by the platform: it overrides `name` |
|
|
81
80
|
| `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 |
|
|
82
81
|
| `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 |
|
|
83
82
|
| `PROTOBASE_TRUSTED_PROXIES` | the proxies in front of `protobase serve`, addresses or CIDR ranges, comma separated, such as `172.30.87.1`, the Docker network's gateway in the [compose template](../guides/deploy.md#compose-template); see [Client addresses](#client-addresses) |
|
|
84
83
|
|
|
85
|
-
- Cookies are `SameSite=Lax`, and `Secure` when `
|
|
84
|
+
- Cookies are `SameSite=Lax`, and `Secure` when the app's `url` is https. When it is http with a port, their names carry it (`better-auth-5173.session_token`), because a browser shares cookies across the ports of a host: two apps on `localhost` then keep their own sign-ins.
|
|
86
85
|
- 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); [Client addresses](#client-addresses) says how a client is known.
|
|
87
86
|
- New passwords need at least 12 characters.
|
|
88
|
-
- `
|
|
87
|
+
- `name`, at the config's top level, is the app's name: authenticator apps and passkey prompts show it, the admin app's sidebar shows it at the top beside the app's [icon](app-identity.md#the-icons), else Protobase's mark (Protobase's wordmark when it is `Protobase`), every browser tab of the admin app ends its title with it, the [installed app](installable-app.md) is called by 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). `PROTOBASE_APP_NAME`, when set, overrides it; without either it is `Protobase`, then without a display name. See [App identity](app-identity.md).
|
|
88
|
+
|
|
89
|
+
### Hooks
|
|
90
|
+
|
|
91
|
+
Sign-in runs the project's own code from `auth/hooks.ts`, which Protobase finds by that path and loads when it builds sign-in. Each hook is a named export, typed with `AuthHooks` from `@protobase/server`, and gets the app's database, `{ db }` (Kysely), as its last argument:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
// auth/hooks.ts
|
|
95
|
+
import { sql } from 'kysely'
|
|
96
|
+
import type { AuthHooks } from '@protobase/server'
|
|
97
|
+
|
|
98
|
+
export const generateUserId: AuthHooks['generateUserId'] = async ({ db }) => {
|
|
99
|
+
const { rows } = await sql<{ id: string }>`select nextval(pg_get_serial_sequence('core.users', 'id'))::text as id`.execute(db)
|
|
100
|
+
return rows[0]!.id
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
| Hook | |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `generateUserId` | the id of a new user, by any route; see [User ids](#roles-and-access-in-the-api) |
|
|
107
|
+
| `onUserCreated` | `(user, context, { db })`, after a user is created by any route: `protobase users create`, an admin, [open sign-up](#open-sign-up) or a [sign-in provider](#sign-in-with-github) |
|
|
108
|
+
| `generateOrganizationId` | the id of a new [organization](#organizations); default a random UUID |
|
|
109
|
+
| `onOrganizationCreated` | `({ organization, member }, { db })`, after an organization is created, with its owner's membership |
|
|
110
|
+
| `beforeOrganizationDeleted` | `(organization, { db })`, before an organization is deleted; throwing refuses it |
|
|
111
|
+
| `organizationHooks` | Better Auth's organization hooks by name, each with `{ db }` last, run after Protobase's own |
|
|
112
|
+
|
|
113
|
+
A mailer of the project's own, for sign-in's mail, is the default export of `auth/mailer.ts` (see [Password reset](#password-reset)). An authenticator of its own, in place of the one `authenticate` describes, is the default export of `auth/authenticate.ts` (see [Tokens and roles](#tokens-and-roles)).
|
|
89
114
|
|
|
90
115
|
### Client addresses
|
|
91
116
|
|
|
@@ -98,7 +123,7 @@ An app served another way than `protobase serve`, such as its own server around
|
|
|
98
123
|
|
|
99
124
|
## First run
|
|
100
125
|
|
|
101
|
-
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 `--no-password` for an account that signs in by emailed code, passkey or a sign-in provider only)
|
|
126
|
+
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 `--no-password` for an account that signs in by emailed code, passkey or a sign-in provider only). The `users` commands call these functions of `@protobase/server`, on the app's sign-in:
|
|
102
127
|
|
|
103
128
|
```ts
|
|
104
129
|
import { createUser, hasUsers, listUsers } from '@protobase/server'
|
|
@@ -190,11 +215,13 @@ PROTOBASE_SMTP_URL=smtps://user:password@smtp.example.com:465 # or smtp://...:
|
|
|
190
215
|
PROTOBASE_MAIL_FROM=noreply@admin.example.com
|
|
191
216
|
```
|
|
192
217
|
|
|
193
|
-
|
|
218
|
+
Sign-in 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 the app at start 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, with a `Mailer` as the default export of `auth/mailer.ts`, or turn mail off whatever the environment says, with `auth: { mailer: false }`:
|
|
194
219
|
|
|
195
220
|
```ts
|
|
196
|
-
|
|
197
|
-
|
|
221
|
+
// auth/mailer.ts
|
|
222
|
+
import type { Mailer } from '@protobase/server'
|
|
223
|
+
|
|
224
|
+
export default { send: async ({ to, subject, text }) => { /* deliver it */ } } satisfies Mailer
|
|
198
225
|
```
|
|
199
226
|
|
|
200
227
|
[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).
|
|
@@ -203,7 +230,7 @@ createAuth({ ..., mailer: false })
|
|
|
203
230
|
- 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.
|
|
204
231
|
- 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.
|
|
205
232
|
- 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.
|
|
206
|
-
- `redirectTo` must be on a trusted origin (`
|
|
233
|
+
- `redirectTo` must be on a trusted origin (the app's `url`, one of its [other origins](#several-origins) or `auth.trustedOrigins`); the admin app sends its own page with a `password-reset` marker, so the link opens the set-password page there.
|
|
207
234
|
- The link is on the origin the reset was asked on, when that is one of the app's.
|
|
208
235
|
- While the [sign-in policy](#sign-in-policy) turns passwords off, reset is off too (`403`).
|
|
209
236
|
|
|
@@ -231,7 +258,7 @@ The [account page](#account-page)'s Devices tab lists the sessions with this one
|
|
|
231
258
|
`leakedPasswords: true` refuses a new password that appears in a known data breach. It is off by default, since it sends a request to an outside service each time a password is set.
|
|
232
259
|
|
|
233
260
|
```ts
|
|
234
|
-
|
|
261
|
+
auth: { leakedPasswords: true }
|
|
235
262
|
```
|
|
236
263
|
|
|
237
264
|
The check is Better Auth's [haveIBeenPwned](https://www.better-auth.com/docs/plugins/have-i-been-pwned) plugin's: only the first five characters of the password's SHA-1 hash go to Have I Been Pwned's Pwned Passwords range API, which answers with every leaked hash that starts with them, so the password itself never leaves the server. It runs wherever someone chooses a password: [open sign-up](#open-sign-up), an [invitation](#organizations), a first password (`/account/set-password`), a change, a [reset](#password-reset), and an admin adding an account or setting a password, `createUser` on the host included. Signing in never checks.
|
|
@@ -261,7 +288,7 @@ With mail configured, the sign-in page offers "Email me a sign-in code": the per
|
|
|
261
288
|
By default the page leads with the password and offers the code below it. For an app whose people mostly never set a password, `signInFirst: 'emailCode'` leads with the code instead: the page asks for the address and "Email me a sign-in code", and the password field, with "Forgot password?", waits behind "Use a password". It applies while the [sign-in policy](#sign-in-policy) leaves both on; `/api/auth/status` then answers `signInFirst: "emailCode"`.
|
|
262
289
|
|
|
263
290
|
```ts
|
|
264
|
-
|
|
291
|
+
auth: { signInFirst: 'emailCode' }
|
|
265
292
|
```
|
|
266
293
|
|
|
267
294
|
- A code works for 10 minutes, once, and three wrong tries end it; asking again sends a new one and the old one stops working. Codes are stored hashed.
|
|
@@ -275,27 +302,27 @@ createAuth({ ..., signInFirst: 'emailCode' })
|
|
|
275
302
|
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:
|
|
276
303
|
|
|
277
304
|
```ts
|
|
278
|
-
|
|
305
|
+
auth: { roles: ['admin', 'member'], signUp: { role: 'member' } }
|
|
279
306
|
```
|
|
280
307
|
|
|
281
308
|
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.
|
|
282
309
|
|
|
283
|
-
- `role` is one of `roles`, and never `admin`:
|
|
310
|
+
- `role` is one of `roles`, and never `admin`: the app stops at start 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.
|
|
284
311
|
- 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.
|
|
285
312
|
- `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.
|
|
286
313
|
- `POST /api/auth/sign-up/verify-code` with `{ email, code }` checks the code on its own and answers `{ ticket }`. A code works for 10 minutes, once, and three wrong tries end it; asking again sends a new one. Codes are stored hashed.
|
|
287
314
|
- `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.
|
|
288
315
|
- 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.
|
|
289
316
|
- 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.
|
|
290
|
-
- `onUserCreated` runs for every account made this way.
|
|
317
|
+
- The `onUserCreated` [hook](#hooks) runs for every account made this way.
|
|
291
318
|
|
|
292
319
|
## Passkeys
|
|
293
320
|
|
|
294
321
|
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/*`.
|
|
295
322
|
|
|
296
|
-
- A passkey is made for the app's passkey domain, its RP ID: the host of `
|
|
297
|
-
- 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 `
|
|
298
|
-
- A passkey's answer, for adding a passkey or signing in, must name the origin of `
|
|
323
|
+
- A passkey is made for the app's passkey domain, its RP ID: the host of the app's `url` (`admin.example.com`), or the host of one of its [other origins](#several-origins) set with `auth.passkeyDomain` or `PROTOBASE_PASSKEY_DOMAIN`. For local development, `http://localhost` works.
|
|
324
|
+
- 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 the app's `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.
|
|
325
|
+
- A passkey's answer, for adding a passkey or signing in, must name the origin of the app's `url` or one of the [other origins](#several-origins) exactly. An origin listed only in `auth.trustedOrigins`, such as a dev tunnel, cannot use passkeys: list it as one of the other origins instead.
|
|
299
326
|
- 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).
|
|
300
327
|
- 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.
|
|
301
328
|
- A passkey sign-in asks for no second step: the passkey is something you have and something you are or know.
|
|
@@ -317,7 +344,7 @@ Sign-in with GitHub or another [provider](#sign-in-with-github) is not asked for
|
|
|
317
344
|
|
|
318
345
|
Some values ask for a sign-in that proved a passkey or a second step not long ago, such as a field with [`.strongSignIn()`](data-config.md#recent-strong-sign-in). Better Auth's sessions do not say how they began, so the server notes it: each passkey sign-in (`/passkey/verify-authentication`), and each correct second step (`/two-factor/verify-totp`, `verify-otp` and `verify-backup-code`), whether it finishes a sign-in or confirms the session the browser has, stores the time beside the session, in the `verification` table, until the session expires. It needs no migration. Staff sessions are never noted.
|
|
319
346
|
|
|
320
|
-
|
|
347
|
+
The app's authenticator gives a token of the person's own session `signIn.strongAt()`, which reads that time when a request needs it, so other requests do no extra lookup. An app with its own authenticator (`auth/authenticate.ts`) sets `signIn: { staff: false, strongAt }` on the sessions it returns, `strongAt` resolving to a `Date` or `undefined`; without it, such values are refused.
|
|
321
348
|
|
|
322
349
|
Confirming it's them later counts from then: a passkey signs the browser in anew, and a code from the authenticator app, or a backup code, confirms the session it has. `createAuthSession` from `@protobase/client` does both, with `signInWithPasskey()` and `verifyTwoFactor({ method: 'totp', code })`.
|
|
323
350
|
|
|
@@ -334,10 +361,10 @@ Admins set how people sign in on the app's **Sign-in policy** page (the profile
|
|
|
334
361
|
| Staff sign-in as a person | On, Email the person, Off | On |
|
|
335
362
|
| Sign in with ... through the platform | On, Off | On |
|
|
336
363
|
|
|
337
|
-
The Default column is the policy of an app that sets none. An app sets its own in
|
|
364
|
+
The Default column is the policy of an app that sets none. An app sets its own in the config, the policy that applies until an admin saves one, such as emailed codes and passkeys only, with the methods it changes:
|
|
338
365
|
|
|
339
366
|
```ts
|
|
340
|
-
|
|
367
|
+
auth: { signInPolicy: { password: 'forbidden' } }
|
|
341
368
|
```
|
|
342
369
|
|
|
343
370
|
It is checked at startup as an admin's save is, so a default that would lock people out stops the app; without mail it applies as a saved policy does (password sign-in is then on). The page shows it, as never changed, until an admin saves a policy. Pair it with `protobase users create --no-password`, so the first admin has no password to fall back on.
|
|
@@ -381,7 +408,7 @@ Admins find the log under the policy on the **Sign-in policy** page: each staff
|
|
|
381
408
|
|
|
382
409
|
Any OpenID Connect provider works. Register the app with it as a confidential client:
|
|
383
410
|
|
|
384
|
-
- Redirect URI: `<
|
|
411
|
+
- Redirect URI: `<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.
|
|
385
412
|
- The client authenticates with its secret (`client_secret_basic`) and PKCE (`S256`); scopes `openid email profile`.
|
|
386
413
|
- The ID token carries `groups`, `amr` (or `acr`) and `auth_time`, signed with a key the provider publishes in its JWKS.
|
|
387
414
|
|
|
@@ -395,28 +422,30 @@ PROTOBASE_OPERATOR_NAME="Protobase Cloud" # shown as "Continue
|
|
|
395
422
|
PROTOBASE_OPERATOR_GROUP=protobase-staff-access # the default
|
|
396
423
|
```
|
|
397
424
|
|
|
398
|
-
Set the first three together or none: some but not all of them stops
|
|
425
|
+
Set the first three together or none: some but not all of them stops the app at start with an error. In the config, `auth.operator` takes the same settings and a few more, and `false` turns staff sign-in off whatever the environment says:
|
|
399
426
|
|
|
400
427
|
```ts
|
|
401
|
-
|
|
402
|
-
|
|
428
|
+
auth: { operator: { issuer, clientId, clientSecret, name: 'Support', group: 'support-leads', scopes: ['groups'], acrValues: ['gold'], sessionMinutes: 15 } }
|
|
429
|
+
auth: { operator: false }
|
|
403
430
|
```
|
|
404
431
|
|
|
405
432
|
`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.
|
|
406
433
|
|
|
407
434
|
## Sign-in with GitHub
|
|
408
435
|
|
|
409
|
-
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:
|
|
436
|
+
Projects have no sign-in provider of their own by default (a platform can add [one](#platform-sign-in-provider)). `auth.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:
|
|
410
437
|
|
|
411
438
|
```ts
|
|
412
|
-
|
|
439
|
+
export default defineConfig(({ env }) => ({
|
|
440
|
+
auth: { socialProviders: { github: { clientId: env.GITHUB_CLIENT_ID!, clientSecret: env.GITHUB_CLIENT_SECRET! } } },
|
|
441
|
+
}))
|
|
413
442
|
```
|
|
414
443
|
|
|
415
|
-
- The callback URL to register with GitHub is `<
|
|
444
|
+
- The callback URL to register with GitHub is `<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`.
|
|
416
445
|
- 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.
|
|
417
446
|
- `GET /api/auth/status` lists the provider ids as `socialProviders`.
|
|
418
447
|
- `encryptOAuthTokens: true` stores the provider's access, refresh and ID tokens encrypted with the secret.
|
|
419
|
-
- `onUserCreated
|
|
448
|
+
- The `onUserCreated` [hook](#hooks) runs after any user is created, a social sign-up included (Better Auth's `databaseHooks.user.create.after`).
|
|
420
449
|
|
|
421
450
|
### Sign-in links and connected accounts
|
|
422
451
|
|
|
@@ -429,17 +458,19 @@ These work for every provider, the project's own and the [platform's](#platform-
|
|
|
429
458
|
|
|
430
459
|
## Sign-in with SAML
|
|
431
460
|
|
|
432
|
-
`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:
|
|
461
|
+
`auth.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:
|
|
433
462
|
|
|
434
463
|
```ts
|
|
435
464
|
import { readFileSync } from 'node:fs'
|
|
436
465
|
|
|
437
|
-
|
|
466
|
+
export default defineConfig({
|
|
467
|
+
auth: { sso: { saml: [{ id: 'okta', name: 'Okta', domains: ['example.com'], idp: { metadata: readFileSync('auth/okta-metadata.xml', 'utf8') } }] } },
|
|
468
|
+
})
|
|
438
469
|
```
|
|
439
470
|
|
|
440
471
|
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`.
|
|
441
472
|
|
|
442
|
-
- **What the provider's admin needs:** the entity ID and metadata URL `<
|
|
473
|
+
- **What the provider's admin needs:** the entity ID and metadata URL `<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.
|
|
443
474
|
- **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.
|
|
444
475
|
- **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 account page does not offer to connect it.
|
|
445
476
|
- `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.
|
|
@@ -463,7 +494,7 @@ PROTOBASE_SIGN_IN_PROVIDER=oidc # the default; the button,
|
|
|
463
494
|
PROTOBASE_SIGN_IN_NAME="Acme SSO" # shown as "Continue with Acme SSO"; default GitHub for github, SSO otherwise
|
|
464
495
|
```
|
|
465
496
|
|
|
466
|
-
Register the app with the provider as a confidential client with the redirect URI `<
|
|
497
|
+
Register the app with the provider as a confidential client with the redirect URI `<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.
|
|
467
498
|
|
|
468
499
|
- **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.
|
|
469
500
|
- **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.
|
|
@@ -471,11 +502,11 @@ Register the app with the provider as a confidential client with the redirect UR
|
|
|
471
502
|
- **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.
|
|
472
503
|
- 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`.
|
|
473
504
|
|
|
474
|
-
Set the first three together or none: some but not all of them stops
|
|
505
|
+
Set the first three together or none: some but not all of them stops the app at start with an error. In the config, `auth.signInProvider` takes the same settings and `scopes`, and `false` turns it off whatever the environment says:
|
|
475
506
|
|
|
476
507
|
```ts
|
|
477
|
-
|
|
478
|
-
|
|
508
|
+
auth: { signInProvider: { issuer, clientId, clientSecret, provider: 'oidc', name: 'Acme SSO', scopes: ['groups'] } }
|
|
509
|
+
auth: { signInProvider: false }
|
|
479
510
|
```
|
|
480
511
|
|
|
481
512
|
### Self-hosting with Dex or Keycloak
|
|
@@ -510,7 +541,7 @@ People sign in with the address their account here has, or connect the provider
|
|
|
510
541
|
|
|
511
542
|
## Roles
|
|
512
543
|
|
|
513
|
-
The roles are a project setting: `
|
|
544
|
+
The roles are a project setting: `auth: { 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).
|
|
514
545
|
|
|
515
546
|
`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.
|
|
516
547
|
|
|
@@ -518,15 +549,14 @@ The roles are a project setting: `createAuth({ ..., roles: ['admin', 'auditor',
|
|
|
518
549
|
|
|
519
550
|
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.
|
|
520
551
|
|
|
521
|
-
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`;
|
|
552
|
+
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`; every store has both columns (see [migrations](#migrations)).
|
|
522
553
|
|
|
523
554
|
## Organizations
|
|
524
555
|
|
|
525
|
-
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`:
|
|
556
|
+
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 `authenticate.tenant`:
|
|
526
557
|
|
|
527
558
|
```ts
|
|
528
|
-
|
|
529
|
-
...,
|
|
559
|
+
auth: {
|
|
530
560
|
roles: {
|
|
531
561
|
admin: 'Superuser',
|
|
532
562
|
manager: { label: 'Manager', grants: ['sales', 'accountant'] },
|
|
@@ -534,25 +564,24 @@ const auth = createAuth({
|
|
|
534
564
|
accountant: 'Accountant',
|
|
535
565
|
},
|
|
536
566
|
organizations: { create: 'everyone' },
|
|
537
|
-
}
|
|
538
|
-
const authenticate = betterAuthAuthenticator({ auth })
|
|
567
|
+
}
|
|
539
568
|
```
|
|
540
569
|
|
|
541
|
-
It is Better Auth's organization plugin, with its tables named in snake_case (`organization`, `member`, `invitation`, `session.active_organization_id`);
|
|
570
|
+
It is Better Auth's organization plugin, with its tables named in snake_case (`organization`, `member`, `invitation`, `session.active_organization_id`); every store has them, and `user.last_organization_id`, with organizations on or off (see [migrations](#migrations)).
|
|
542
571
|
|
|
543
572
|
**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.
|
|
544
573
|
|
|
545
574
|
**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`.
|
|
546
575
|
|
|
547
|
-
**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; a removed member's token, or one carrying an app role taken away, [stops working](#when-a-token-stops-working) on its next request.
|
|
576
|
+
**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; a removed member's token, or one carrying an app role taken away, [stops working](#when-a-token-stops-working) on its next request. The app's authenticator 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 every organization stays a resource without `.tenant`, readable by the global role; a list across the organizations someone is a member of is a `.tenant` resource with `across: 'memberships'` (see [Lists across organizations](access.md#lists-across-organizations)).
|
|
548
577
|
|
|
549
|
-
**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 `{
|
|
578
|
+
**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 `{url}/-/invitation?token=…` (`invitationUrl`), with a token signed with the app's `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. Someone signed in sees the invitations for their own address with Better Auth's `GET /api/auth/organization/list-user-invitations`, on the organizations page, and accepts one there with `POST /api/auth/invitation/accept-own` (`{ invitationId }`). 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.
|
|
550
579
|
|
|
551
580
|
**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.
|
|
552
581
|
|
|
553
|
-
**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. `
|
|
582
|
+
**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. The `beforeOrganizationDeleted` [hook](#hooks) may refuse too.
|
|
554
583
|
|
|
555
|
-
**Ids.** `
|
|
584
|
+
**Ids.** The `generateOrganizationId` [hook](#hooks) 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 `onOrganizationCreated`, as the ERP example does (`examples/erp/auth/hooks.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.
|
|
556
585
|
|
|
557
586
|
The app's own pages are under the profile menu: the switcher, "Manage 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`, ...).
|
|
558
587
|
|
|
@@ -560,13 +589,13 @@ The app's own pages are under the profile menu: the switcher, "Manage organizati
|
|
|
560
589
|
|
|
561
590
|
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. No other answer under `/api/auth` carries the session token (Better Auth's `token` and `session.token`, as in a sign-in, `get-session` or `list-sessions`): the session is the httpOnly cookie's alone, so script on the page never holds it.
|
|
562
591
|
|
|
563
|
-
`
|
|
592
|
+
The app's authenticator, which the config's `authenticate` describes, 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 `authenticate.tenant`, 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 (`authenticate: { cache: { refreshMs, minReloadMs } }`). An API that runs apart from the auth server sets `authenticate: { jwksUrl, issuer }`, with `organizations: true` when its tokens carry organizations; that turns its own sign-in off, unless the config sets `auth` too. An authenticator of the app's own, such as `jwtAuthenticator(...)` from `@protobase/server` for the tokens of another system, is the default export of `auth/authenticate.ts`, which replaces the one `authenticate` describes.
|
|
564
593
|
|
|
565
594
|
`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.
|
|
566
595
|
|
|
567
596
|
### When a token stops working
|
|
568
597
|
|
|
569
|
-
With
|
|
598
|
+
With sign-in on, the app's authenticator checks on every request that the token still stands, so these take effect on the next request every route that authenticates sees (the resources API, `/api/meta`, batches, functions, files and new `/live` streams), not when the token expires:
|
|
570
599
|
|
|
571
600
|
| Change | The token |
|
|
572
601
|
| --- | --- |
|
|
@@ -578,14 +607,14 @@ With `auth`, `betterAuthAuthenticator` checks on every request that the token st
|
|
|
578
607
|
|
|
579
608
|
A refused token is a `401`, as an expired one is, and the client retries the request once with a new token from `/api/auth/token`. Tokens carry their session's id in `sid`; one from [`issueToken`](#tokens-for-scripts-and-curl) names none, and is checked against the user alone.
|
|
580
609
|
|
|
581
|
-
The cost is two or three lookups by key in the auth store per request: the session by `sid`, the user by `sub`, and with organizations the membership. Nothing is cached, so every server process sees a change at once. An open `/live` stream was admitted when it connected and keeps running until it closes. With `jwksUrl` instead of
|
|
610
|
+
The cost is two or three lookups by key in the auth store per request: the session by `sid`, the user by `sub`, and with organizations the membership. Nothing is cached, so every server process sees a change at once. An open `/live` stream was admitted when it connected and keeps running until it closes. With `authenticate.jwksUrl` instead of sign-in of its own, the API cannot read the auth store and a token works until it expires, as do tokens of [other apps](#users-of-another-app).
|
|
582
611
|
|
|
583
612
|
## Users of another app
|
|
584
613
|
|
|
585
|
-
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). `
|
|
614
|
+
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). `authenticate.apps` lists each such app by its `url`, the issuer of its tokens, with the roles its users' global roles become here:
|
|
586
615
|
|
|
587
616
|
```ts
|
|
588
|
-
|
|
617
|
+
authenticate: { apps: [{ issuer: 'https://shop.example.com', roles: { admin: 'shop' }, tenant: 1 }] }
|
|
589
618
|
```
|
|
590
619
|
|
|
591
620
|
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).
|
|
@@ -595,12 +624,12 @@ Their tokens are verified against that app's keys at `<issuer>/api/auth/jwks`; a
|
|
|
595
624
|
An app moving from Supabase keeps its `supabase.auth` calls and its `@supabase/ssr` session checks: the app serves GoTrue's endpoints at `/api/sb/auth/v1`, over the same Better Auth sign-in, so `createClient(<the app's origin>/api/sb, <publishable key>)` signs in, refreshes, reads the user and signs out as on Supabase.
|
|
596
625
|
|
|
597
626
|
- `signInWithPassword`, `signInWithOtp` then `verifyOtp({ email, token, type: 'email' })`, `signInWithOAuth` with one of the app's [sign-in providers](#sign-in-with-github) (PKCE, as `@supabase/ssr` uses, or the implicit flow), `refreshSession`, `getUser`, `getClaims`, `updateUser({ password, data })` and `signOut` work. Each goes through Better Auth's own endpoint, with its rate limits and the [sign-in policy](#sign-in-policy).
|
|
598
|
-
- The implicit flow is for an https `redirect_to` whose exact origin is a trusted origin. For http, including localhost, custom schemes and hosts that only a wildcard pattern in `
|
|
627
|
+
- The implicit flow is for an https `redirect_to` whose exact origin is a trusted origin. For http, including localhost, custom schemes and hosts that only a wildcard pattern in `auth.trustedOrigins` matches, use PKCE: supabase-js `flowType: 'pkce'`; `@supabase/ssr` already uses it.
|
|
599
628
|
- The sign-in must start at `/api/sb/auth/v1/authorize` in the same browser: `/api/sb/auth/v1/callback` hands a code or a session only at the end of a provider sign-in started there.
|
|
600
629
|
- The browser keeps no Protobase session afterwards: the app holds its own.
|
|
601
|
-
- A wildcard pattern in `
|
|
630
|
+
- A wildcard pattern in `auth.trustedOrigins` still trusts every host it matches with the sign-in code, so list only hosts you control.
|
|
602
631
|
- Addresses are proved with the emailed 6-digit code, never a link. `signUp({ email, password, options: { data } })` needs [open sign-up](#open-sign-up) and mails a sign-up code; `verifyOtp({ email, token, type: 'signup' })` makes the account, with `data` in the [user metadata column](#an-existing-users-table), and signs in. Until then there is no account and `signUp` answers no user. `resetPasswordForEmail` mails a sign-in code; `verifyOtp` with `type: 'recovery'` signs in, and `updateUser({ password })` sets the new password.
|
|
603
|
-
- The access token is ES256, valid for an hour, with Supabase's claims: `sub`, `role: authenticated`, `aud: authenticated`, `email`, `app_metadata`, `user_metadata`, `aal`, `session_id`, issued by `<
|
|
632
|
+
- The access token is ES256, valid for an hour, with Supabase's claims: `sub`, `role: authenticated`, `aud: authenticated`, `email`, `app_metadata`, `user_metadata`, `aal`, `session_id`, issued by `<url>/api/sb/auth/v1`. Its keys are at `/api/sb/auth/v1/.well-known/jwks.json`. It is apart from the EdDSA token of [Tokens and roles](#tokens-and-roles): neither is accepted for the other.
|
|
604
633
|
- The refresh token is the Better Auth session's token. Each refresh gives the session a new one, so a refresh token works once, and signing out, a ban or a revoked session ends it.
|
|
605
634
|
- `email_confirmed_at` is the time the address was confirmed, in the user table's `email_confirmed_at` column. An account verified before that column existed has none.
|
|
606
635
|
- Not offered:
|
|
@@ -629,14 +658,14 @@ It is signed with the same keys and carries the same claims as a token from `/ap
|
|
|
629
658
|
With `mcp`, the app is an OAuth provider for MCP clients, such as AI assistants and code editors that speak MCP: a person adds the app's MCP address to the client, the client registers itself, the person signs in on the app's own pages and allows it on the connect page, and the client gets tokens that act as that person. What the client can do with them is in [MCP](mcp.md).
|
|
630
659
|
|
|
631
660
|
```ts
|
|
632
|
-
|
|
633
|
-
|
|
661
|
+
auth: { mcp: true }
|
|
662
|
+
auth: { mcp: { path: '/api/mcp', connectPage: '/-/connect' } }
|
|
634
663
|
```
|
|
635
664
|
|
|
636
|
-
- `path` is the MCP endpoint's path on the origin of `
|
|
637
|
-
- It needs an https `
|
|
665
|
+
- `path` is the MCP endpoint's path on the origin of the app's `url`, its OAuth resource (default `/api/mcp`); `connectPage` is the connect page's path (default `/-/connect`, for a UI at the root). A client's metadata document is fetched through a transport that refuses private and reserved addresses.
|
|
666
|
+
- It needs an https `url`, or localhost. Every store has the OAuth tables (see [migrations](#migrations)), so turning MCP on or off needs no migration. Turning it off drops nothing: the clients and grants stay, and work again once MCP is on, so disconnect the clients first to forget them. While MCP is off, ending a person's sessions, banning them or setting their password disconnects none of their clients. Clients connect through the origin of `url` only, whatever [other origins](#several-origins) the app has. Changing `path` makes every client connect again.
|
|
638
667
|
- Clients find the provider at `/.well-known/oauth-protected-resource` and `/.well-known/oauth-authorization-server`, register at `/api/auth/oauth2/register` (dynamic registration) or are known by an https client id that names their metadata document (CIMD), and are asked for nothing per client. Back-channel logout, a key set URL at registration (`jwks_uri`) and the client credentials grant are refused, though a client with a metadata document may publish its keys on its own origin, and the provider's client management, userinfo, logout and consent endpoints are closed.
|
|
639
|
-
- The scopes are `mcp` and `offline_access`, which is added when a client leaves it out. Access tokens last 15 minutes, carry the MCP resource as their audience and `typ: at+jwt`, and work for MCP only: the app's API refuses them, and the MCP endpoint refuses the app's own tokens. Refresh tokens roll for 30 days.
|
|
668
|
+
- The scopes are `mcp` and `offline_access`, which is added when a client leaves it out. Access tokens last 15 minutes, carry the MCP resource as their audience and `typ: at+jwt`, and work for MCP only: the app's API refuses them, and the MCP endpoint refuses the app's own tokens. Refresh tokens roll for 30 days. The MCP endpoint verifies them, and checks on every request that the person still allows the client, still exists unbanned and still holds each role the token carries, so a disconnected client, a ban, a lowered role or a deleted user stops its access tokens at the next request rather than at their expiry; a lookup that fails refuses the request. A client that binds its tokens to a key (DPoP) sends a proof with each, checked against the resource's URL.
|
|
640
669
|
- A grant keeps the role and organization the person worked in when they allowed the client. A refresh is refused once they no longer hold that role or may no longer enter that organization, once the [sign-in policy](#sign-in-policy) requires something they have not set up, and while they are banned; access tokens already issued last out their 15 minutes. A grant made without a role is refused.
|
|
641
670
|
- Someone who has not set up what the sign-in policy requires is sent to the setup page first. Staff signed in as a person cannot allow a client, nor disconnect one. An account whose address is not verified cannot connect a client until it signs in once with an emailed code: a provider sign-up whose provider did not vouch for the address, or a password account created before Protobase created accounts verified. With password-only sign-in, or without mail, it cannot.
|
|
642
671
|
- The connect page shows the client's name as the client's own claim, who published it for a client with an https id, where the code goes (a website by its host, an app on this computer by its port, or an app on the device by its whole address), and the signed-in address. It cannot be used inside another site's frame, and Allow takes a click only once the page has been in front for half a second. `protobase serve` and `protobase dev` send `X-Frame-Options: DENY` and `Content-Security-Policy: frame-ancestors 'none'` with every page of the UI; a host that serves `public/` itself should send them too.
|
|
@@ -646,17 +675,18 @@ createAuth({ ..., mcp: { path: '/api/mcp', connectPage: '/-/connect' } })
|
|
|
646
675
|
|
|
647
676
|
## Several origins
|
|
648
677
|
|
|
649
|
-
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:
|
|
678
|
+
An app can be served on more than one origin, such as its platform address and the customer's own domain. `auth.origins` lists the others; it defaults to `PROTOBASE_ORIGINS`, which a platform sets:
|
|
650
679
|
|
|
651
680
|
```ts
|
|
652
|
-
|
|
681
|
+
url: 'https://erp-x1.protobase.net',
|
|
682
|
+
auth: { origins: ['https://admin.shop.com'], passkeyDomain: 'admin.shop.com' },
|
|
653
683
|
```
|
|
654
684
|
|
|
655
|
-
- 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 `
|
|
656
|
-
- Tokens keep `
|
|
685
|
+
- 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 the app's `url`.
|
|
686
|
+
- Tokens keep the app's `url` as issuer and audience, whichever origin issued them, so the API takes them on every origin.
|
|
657
687
|
- Sessions are cookies of each host: a person signs in once per origin.
|
|
658
688
|
- `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.
|
|
659
|
-
- Each origin is a URL with the scheme of `
|
|
689
|
+
- Each origin is a URL with the scheme of the app's `url`; one that is not stops the server at startup. The host that serves the app must pass each origin's own `Host` header on.
|
|
660
690
|
- The app's own origins may call each other's API from the browser ([CORS](api.md#cors)).
|
|
661
691
|
|
|
662
692
|
## Public endpoints
|
|
@@ -665,6 +695,6 @@ Only the Better Auth routes under `/api/auth/*` (sign-in, password reset, [open
|
|
|
665
695
|
|
|
666
696
|
## Roles and access in the API
|
|
667
697
|
|
|
668
|
-
Roles are bundles of capabilities (`defineRoles` in `@protobase/schema`: `orders.read.own`, `costs.read`, `*.read`, ...).
|
|
698
|
+
Roles are bundles of capabilities (`defineRoles` in `@protobase/schema`: `orders.read.own`, `costs.read`, `*.read`, ...). Export the result as `roles` from `config/roles.ts`, where Protobase finds it, 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).
|
|
669
699
|
|
|
670
|
-
**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
|
|
700
|
+
**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 the `generateUserId` [hook](#hooks) the next value of that table's sequence, and adds the user's row there, as the ERP example does (`examples/erp/auth/hooks.ts`, with the row added by `db/migrations/019_auth_users.sql` when someone joins an organization). One whose owner columns are UUIDs sets `auth: { generateUserId: 'uuid' }` (see [An existing users table](#an-existing-users-table)). An authenticator that maps the signed-in account to a user of the app's own, such as one in `auth/authenticate.ts` that returns the id of a row in its users table, declares the type of those ids instead, `Object.assign(authenticate, { userIdType: 'uuid' })` (or `'integer'`, `'bigint'`, `'text'`), and the owner fields are checked against that. 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.
|