Skip to content

Sign-in options

The keys of sign-in and of how the API verifies tokens, in protobase.config.ts. The app’s URL, secrets and name, which sign-in uses, are the top-level url, secret, previousSecrets and name. How sign-in works is in Login with Better Auth.

  • Type: AuthConfig | false
  • Default: on, with the defaults below; off when authenticate.jwksUrl trusts an auth server elsewhere

Sign-in, with its store in the app’s own database. false turns it off; an app without sign-in then verifies tokens with authenticate or auth/authenticate.ts.

export default defineConfig({ auth: { roles: ['admin', 'sales'], signInFirst: 'emailCode' } })

Sign-in runs the project’s own code from these files, each loaded when Protobase builds sign-in:

  • auth/hooks.ts: the hooks, as named exports typed with AuthHooks from @protobase/server: generateUserId, onUserCreated, generateOrganizationId, onOrganizationCreated, beforeOrganizationDeleted and organizationHooks. Each gets the app’s database, { db }, as its last argument. See Hooks.
  • auth/mailer.ts: a mailer of the project’s own, as its default export, in place of the SMTP mailer: sign-in’s mail and the jobs’ mail both go through it. See Email.

With extensions, the hooks and the mailer are those of the last folder whose config sets auth.

  • Type: string[] | Record<string, string | { label: string; grants?: string[] }>
  • Default: ['admin', 'user']

The roles users can hold. admin is always included. With organizations, give each a label, and optionally the other roles its holders may also give members. The capabilities each role has are config/roles.ts’s (see Access). See Roles.

export default defineConfig({
auth: { roles: { manager: { label: 'Manager', grants: ['sales'] }, sales: 'Sales' }, organizations: true },
})
  • Type: string
  • Default: none; with exactly one role besides admin, that role

The role of a user created without one. Without a default, and with more than one role besides admin, creating a user without a role is an error that asks for one.

export default defineConfig({ auth: { roles: ['admin', 'sales', 'warehouse'], defaultRole: 'sales' } })
  • Type: true | { create?: 'admins' | 'everyone'; names?: { singular: string; plural: string }; createDescription?: string; creatorAppRoles?: 'all' | string[]; invitationDays?: number; membershipLimit?: number; deleteRecords?: 'refuse' | 'cascade'; invitationUrl?: string }
  • Default: off: the app works as one organization

Turns on organizations: people work in one at a time, records of .tenant resources are scoped to it, and roles are held globally or per membership. create is who may create one (default admins), names what the screens call an organization, as a view’s names are written (default Organization), createDescription a line under the title of the screen where someone names their first, creatorAppRoles the roles its creator holds there (default all), invitationDays how long an invitation works (default 7), membershipLimit the most members (default 1000), deleteRecords whether deleting one is refused while it has records (refuse, the default) or left to the foreign keys (cascade), and invitationUrl the page an invitation opens (default /-/invitation on the origin the invitation was made on). Its hooks are in auth/hooks.ts. See Organizations and First organization.

export default defineConfig({ auth: { organizations: { create: 'everyone', invitationDays: 14, names: { singular: 'Workspace', plural: 'Workspaces' } } } })
  • Type: { role: string; domains?: string[]; fields?: ('email' | 'name')[] }
  • Default: off

Open sign-up: anyone makes their own account from the sign-in page, with role, after proving their address with an emailed code. Needs a mailer. role is one of roles and never admin. domains takes only addresses at those email domains, subdomains included, and PROTOBASE_SIGN_UP_DOMAINS replaces it; an empty list leaves sign-up off. fields is what it asks for, ['email', 'name'] by default; ['email'] has the emailed sign-in code make the account of a new address. Not with organizations. See Open sign-up, Sign-up for email domains and Sign-up with the address only.

export default defineConfig({ auth: { roles: ['admin', 'customer'], signUp: { role: 'customer' } } })
  • Type: Partial<{ password: 'allowed' | 'forbidden'; emailCode: 'allowed' | 'forbidden'; passkey: 'allowed' | 'required' | 'forbidden'; twoFactor: 'allowed' | 'required' | 'forbidden'; staffAccess: 'allowed' | 'notify' | 'forbidden'; platformSignIn: 'allowed' | 'forbidden' }>
  • Default: everything allowed

The sign-in policy that applies until an admin saves one on the Sign-in policy page, and when the saved one no longer reads. Checked at startup as an admin’s save is. See Sign-in policy.

export default defineConfig({ auth: { signInPolicy: { password: 'forbidden', twoFactor: 'required' } } })
  • Type: 'password' | 'emailCode'
  • Default: password

What the sign-in page leads with: the address and a password, with emailing a code below it; or the address and “Email me a sign-in code”, with the password behind “Use a password”. Only while the policy leaves both on. See Emailed sign-in codes.

export default defineConfig({ auth: { signInFirst: 'emailCode' } })
  • Type: number
  • Default: 5

Sign-in attempts allowed per minute and client, and the sign-up requests allowed. See Client addresses for how a client is known.

export default defineConfig({ auth: { signInPerMinute: 10 } })
  • Type: false
  • Default: SMTP with PROTOBASE_SMTP_URL and PROTOBASE_MAIL_FROM, when set
  • Environment variables: PROTOBASE_SMTP_URL, PROTOBASE_MAIL_FROM

false turns sign-in’s mail off whatever the environment says: password reset, emailed codes, sign-up, staff sign-in and invitations are then not offered. A mailer of the project’s own is auth/mailer.ts. See Email.

export default defineConfig({ auth: { mailer: false } })
  • Type: Better Auth’s socialProviders
  • Default: none

Sign-in providers, such as { github: { clientId, clientSecret } }; the callback is <url>/api/auth/callback/<provider>. A provider also signs up people without an account, with the default role. See Sign-in with GitHub.

export default defineConfig(({ env }) => ({
auth: { socialProviders: { github: { clientId: env.GITHUB_CLIENT_ID!, clientSecret: env.GITHUB_CLIENT_SECRET! } } },
}))
  • Type: 'gmail'[]
  • Default: none

The mail providers people may link their own mailbox with, for the app’s functions to send mail from it; each links through its sign-in provider in socialProviders, Gmail through google. It needs auth.encryptOAuthTokens: without it, startup stops, since the linked mailboxes’ tokens would be stored in plain text. See From someone’s own mailbox.

export default defineConfig(({ env }) => ({
auth: { socialProviders: { google: { clientId: env.GOOGLE_CLIENT_ID!, clientSecret: env.GOOGLE_CLIENT_SECRET! } }, personalMail: ['gmail'], encryptOAuthTokens: true },
}))
  • Type: boolean
  • Default: false

Encrypts the OAuth access and refresh tokens kept for social sign-ins, with the app’s secret. auth.personalMail needs it.

export default defineConfig({ auth: { encryptOAuthTokens: true } })
  • Type: { saml: { id: string; name?: string; domains: string[]; idp: { metadata: string } | { entityId: string; ssoUrl: string; certificate: string | string[] }; attributes?: { email?: string; name?: string; firstName?: string; lastName?: string } }[] }
  • Default: none

Single sign-on with SAML 2.0 identity providers. A provider signs up people without an account, with the default role, but only at its domains, where it also links to the account with the same address. name defaults to SSO. See Sign-in with SAML.

export default defineConfig(({ env }) => ({
auth: { sso: { saml: [{ id: 'okta', name: 'Okta', domains: ['example.com'], idp: { metadata: env.OKTA_METADATA! } }] } },
}))
  • Type: { issuer: string; clientId?: string; clientSecret?: string; provider?: string; name?: string; scopes?: string[]; authorizationParams?: Record<string, string>; tokenSignIn?: boolean; hidden?: boolean; button?: boolean; devices?: boolean }[]
  • Default: the providers of the environment variables below, when set
  • Environment variables: PROTOBASE_SIGN_IN_ISSUER, PROTOBASE_SIGN_IN_CLIENT_ID, PROTOBASE_SIGN_IN_CLIENT_SECRET, and optionally PROTOBASE_SIGN_IN_PROVIDER, PROTOBASE_SIGN_IN_NAME, PROTOBASE_SIGN_IN_AUTHORIZATION_PARAMS (name=value&other=value), PROTOBASE_SIGN_IN_TOKENS, PROTOBASE_SIGN_IN_HIDDEN, PROTOBASE_SIGN_IN_BUTTON and PROTOBASE_SIGN_IN_DEVICES (true or false); the same with _2_, _3_ and so on after PROTOBASE_SIGN_IN for each further provider

The OpenID Connect providers the platform adds for people who already have an account; they never sign anyone up. Each has its own provider id (default oidc) and name people see; authorizationParams go on every authorization request to it, unchangeable by the client; tokenSignIn: true lets a client sign in with a fresh ID token it issued for this app (Token sign-in, default off); hidden: true keeps its links off the account page; button: false keeps its button off the sign-in page, so only a sign-in link or another app’s redirect starts it; devices: true makes each device an account of its own (Devices). [] turns them off whatever the environment says. One is left out when socialProviders has a provider with its id. Without clientSecret one signs in by token sign-in alone, which tokenSignIn: true must then turn on, with tokens for the origin of url unless clientId names another. See Platform sign-in providers.

export default defineConfig({ auth: { signInProviders: [] } })
  • Type: { issuer: string; clientId: string; clientSecret: string; name?: string; group?: string; scopes?: string[]; acrValues?: string[]; sessionMinutes?: number } | false
  • Default: the provider of the environment variables below, when set
  • Environment variables: PROTOBASE_OPERATOR_ISSUER, PROTOBASE_OPERATOR_CLIENT_ID, PROTOBASE_OPERATOR_CLIENT_SECRET, and optionally PROTOBASE_OPERATOR_NAME and PROTOBASE_OPERATOR_GROUP

The identity provider the operator’s staff sign in with to sign in as people of this app, for support. group is the ID token’s group that allows it (default protobase-staff-access), and sessionMinutes how long a staff session lasts (default 30, at most 240). false turns staff sign-in off whatever the environment says. See Staff sign-in.

export default defineConfig({ auth: { operator: false } })
  • Type: 'bcrypt'[]
  • Default: none

Password hashes another system wrote that sign-in accepts besides Better Auth’s own, such as Supabase Auth’s bcrypt. The first sign-in with one stores the password again as Better Auth hashes it. See An existing users table.

export default defineConfig({ auth: { passwordHashes: ['bcrypt'] } })
  • Type: boolean
  • Default: false

Refuses a new password that appears in a known data breach, by Have I Been Pwned’s list. Only the first five characters of the password’s SHA-1 hash leave the server. See Leaked passwords.

export default defineConfig({ auth: { leakedPasswords: true } })
  • Type: boolean
  • Default: true

Lets the signed-in person delete their own account from the account page, as app stores require of an app that makes accounts. false turns it off, for an app whose accounts an organization’s admins manage. See Deleting the account.

export default defineConfig({ auth: { accountDeletion: false } })
  • Type: { prefix?: string }, and for each built-in table, by its name, { name?: string; columns?: { [column]?: string } } with its built-in columns as keys
  • Default: the built-in names

The names of the auth store’s tables and columns in the database, where the app wants others than the built-in ones: prefix goes before the name of every table it does not name, and each table, by its built-in name, takes its own name and columns, each column by its built-in name. id keeps its name. A name past Postgres’s 63 bytes is refused. See Table and column names.

export default defineConfig({
auth: { tables: { prefix: 'app_', user: { name: 'users', columns: { role: 'app_role' } } } },
})
  • Type: { metadata?: string; keyTable?: { schema: string; table: string }; guardedSchemas?: string[] }
  • Default: none

The user table, when another system made it, such as Supabase’s auth.users: a JSON column for each user’s metadata, and what else the store must know of it; auth.tables.user names it and its columns. See An existing users table. keyTable makes the user table a view of the app’s own over another system’s, the table it names, which holds the user ids: the store’s migrations leave the view to the app and point their foreign keys at that table. See A view over another system’s users. guardedSchemas names the schemas the other system keeps the rest of its sign-in data in, such as Supabase Auth’s auth: no resource may be over any table in them, and the assistant’s queries never read one.

export default defineConfig({
auth: { schema: 'auth', tables: { user: { name: 'users', columns: { role: 'app_role' } } }, users: { metadata: 'raw_user_meta_data', guardedSchemas: ['auth'] } },
})
  • Type: string
  • Default: auth

The schema of the auth store in the app’s database. See Admin store.

export default defineConfig({ auth: { schema: 'accounts' } })
  • Type: 'uuid'
  • Default: Better Auth’s random 32-character text

'uuid' gives new users a random UUID, and makes every id column of the store a uuid. To take ids from the app instead, such as its own users sequence, export generateUserId from auth/hooks.ts.

export default defineConfig({ auth: { generateUserId: 'uuid' } })
  • Type: string[]
  • Default: PROTOBASE_ORIGINS, comma separated; none otherwise
  • Environment variable: PROTOBASE_ORIGINS

The other origins the app is served on, besides url. A request to one is answered for that host: its sign-in callbacks and the links it mails stay there, and passkeys work on every origin. Tokens keep url as issuer. See Several origins.

export default defineConfig({ auth: { origins: ['https://admin.shop.com'] } })
  • Type: string
  • Default: PROTOBASE_PASSKEY_DOMAIN, else the host of url
  • Environment variable: PROTOBASE_PASSKEY_DOMAIN

The host new passkeys are made for: the host of url or of one of origins. Each passkey keeps the one it was made for, so changing it breaks none. See Passkeys.

export default defineConfig({ auth: { origins: ['https://admin.shop.com'], passkeyDomain: 'admin.shop.com' } })
  • Type: string[]
  • Default: PROTOBASE_TRUSTED_ORIGINS, comma separated
  • Environment variable: PROTOBASE_TRUSTED_ORIGINS

Extra origins that may call the auth endpoints, such as the exact host of a dev tunnel; none is trusted by default. An entry with *, such as https://*.example.com, trusts every host it matches, so list only hosts you control. url and origins always are. Set in the config, it replaces the variable’s list.

export default defineConfig(({ env }) => ({
auth: { trustedOrigins: env.TUNNEL_URL ? [env.TUNNEL_URL] : undefined },
}))
  • Type: true | { path?: string; connectPage?: string }
  • Default: off

Turns on the OAuth provider for MCP clients, and with it the MCP endpoint: a client registers itself, the person signs in and allows it on the connect page, and the client gets tokens for the MCP endpoint only. path is the endpoint’s (default /api/mcp), connectPage the page’s (default /-/connect). Needs an https url, or localhost, and the OAuth tables from protobase auth migration. The endpoint’s tools are mcpEndpoint. See MCP.

export default defineConfig({ auth: { mcp: true } })
  • Type: { api?: boolean }
  • Default: off

What the OAuth provider of auth.mcp serves besides MCP. api: true lets clients get access tokens for the app’s API, such as a phone or watch app that signs people in on the connect page, which the API takes as Authorization: Bearer <token>. Needs auth.mcp; the server refuses to start without it. See OAuth tokens for the API.

export default defineConfig({ auth: { mcp: true, oauth: { api: true } } })
  • Type: true | { connectPage?: string; audiences?: string[]; subject?: 'user' | 'grant'; clients?: { clientId: string; name: string; redirectUris: string[]; type: 'public'; exchange?: boolean }[] }
  • Default: off

Makes the app an OpenID provider for the clients its admins register at /api/auth/openid-provider/clients and those clients declares, such as other apps that sign people in with it: they get an ID token with the person’s sub, email and email_verified, without asking the person to allow them, and nothing else can register. A client allowed to exchange gets ID tokens for the other clients, and for the origins audiences names as patterns such as https://*.example.app, without a browser. connectPage is the page people sign in on (default /-/connect). subject: 'grant' makes an ID token’s sub the client’s grant, new on each fresh sign-in and kept across its refresh tokens and exchanges, never the person’s id (Grant subjects; default user). clients declares the app’s own public clients, such as its phone app, under the ids written there, with the fields the admin API takes: they work from the first request on, with no admin registering them, and stop once removed (Declared clients). Not with auth.mcp; the server refuses to start with both. See OpenID provider for other apps.

export default defineConfig({ auth: { openIdProvider: true } })
export default defineConfig({ auth: { openIdProvider: { clients: [{ clientId: 'example-phone', name: 'Phone', redirectUris: ['com.example.phone:/signed-in'], type: 'public', exchange: true }] } } })
  • Type: { prefix: string }
  • Default: off

Turns on personal access tokens: a signed-in person makes long-lived tokens on their account page, for a script to call the app’s API as them with Authorization: Bearer <token>. prefix starts every token, lowercase letters, digits and underscores ending in _; it has no default, and the server refuses to start without it or with another. See Personal access tokens.

export default defineConfig({ auth: { personalAccessTokens: { prefix: 'invoices_' } } })
  • Type: AuthenticateSettings
  • Default: the tokens of auth

How the API verifies tokens: the app’s own, those of an auth server elsewhere, and those of other Protobase apps it admits. An authenticator of the project’s own, such as jwtAuthenticator(...) for tokens of another system, is the default export of auth/authenticate.ts, which replaces this key. See Tokens and roles.

export default defineConfig({ authenticate: { tenant: 'acme' } })
  • Type: string | number

The one tenant everyone works in, for an app without organizations; never read from the token.

export default defineConfig({ authenticate: { tenant: 1 } })
  • Type: string

The key set of an auth server elsewhere, for an API that runs apart from it. Needs issuer. Set without auth, it turns the app’s own sign-in off.

export default defineConfig({
authenticate: { jwksUrl: 'https://auth.example.com/api/auth/jwks', issuer: 'https://auth.example.com' },
})
  • Type: string
  • Default: the app’s url

The issuer the tokens must name; it is also their audience unless audience names another.

export default defineConfig({ authenticate: { jwksUrl: 'https://auth.example.com/api/auth/jwks', issuer: 'https://auth.example.com' } })
  • Type: string
  • Default: issuer

The audience the tokens must name.

export default defineConfig({ authenticate: { jwksUrl: 'https://auth.example.com/api/auth/jwks', issuer: 'https://auth.example.com', audience: 'https://api.example.com' } })
  • Type: boolean
  • Default: read from auth

Whether the tokens carry organizations. Needed with jwksUrl only.

export default defineConfig({ authenticate: { jwksUrl: 'https://auth.example.com/api/auth/jwks', issuer: 'https://auth.example.com', organizations: true } })
  • Type: { refreshMs?: number; minReloadMs?: number }
  • Default: { refreshMs: 600000, minReloadMs: 30000 }

How long the app’s own key set is kept, when the tokens are those of auth: reloaded whole every refreshMs, so removed keys stop working, and when a token names an unknown key, at most every minReloadMs. A key set at jwksUrl is cached by its own rules, and cache does not change them.

export default defineConfig({ authenticate: { cache: { refreshMs: 60_000 } } })
  • Type: { issuer: string; roles: Record<string, string | string[]>; tenant?: string | number }[]
  • Default: none

Other Protobase apps whose signed-in users this API admits with their own tokens, so their admin apps can draw its resources. issuer is that app’s url, roles maps its global roles to this app’s (a role it does not list is dropped, and a user left with none is refused), and tenant is where its users work. See Users of another app.

export default defineConfig({
authenticate: { apps: [{ issuer: 'https://admin.shop.com', roles: { admin: 'shop' } }] },
})
  • Type: number
  • Default: 30

Days a token signing key signs; the first token after that makes a new key. A month bounds how long a leaked key could sign, and keeps the key set to a few keys. A top-level key, beside auth. See Signing keys.

export default defineConfig({ signingKeyDays: 90 })
  • Type: number, at least 1
  • Default: 7

Days a replaced signing key stays in the key set, so the tokens it signed keep verifying until they expire; it is deleted after that. A week is far longer than any token lasts, a day at most, so an API that caches the key set never refuses a token signed just before a rotation. A top-level key, beside auth.

export default defineConfig({ signingKeyGraceDays: 14 })