Skip to content
Spools
Documentation
On this page

Signing in

Two separate things decide who gets into a Spool.

  • The auth adapter says who someone is. You choose it in src/spool.server.ts, and its browser half in src/spool.ui.ts.
  • The access list in crm.config.json says whether they may come in. It compares their email address, and only one the auth provider has verified as theirs, with access.allowedEmails and access.allowedDomains. Anyone else who signs in sees a page saying they have no access.
json
"access": { "allowedDomains": ["acme.com"], "allowedEmails": ["accountant@example.org"] }

Answers are kept for a minute, so taking someone off the list (or out of the provider) takes effect within one.

Adapter When
clerk() You want sign-in run for you
betterAuth() You want it in your own database, with no outside service
devAuth() Development, tests and demos. Refuses to run in production

Clerk#

ts
// src/spool.server.tsimport { clerk } from "@swirls/spools/auth/clerk";auth: clerk(),// src/spool.ui.tsimport { clerkClient } from "@swirls/spools/auth/clerk/ui";export const authClients = [clerkClient];

Set CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY, and install @clerk/tanstack-react-start. Under Restrictions in the Clerk dashboard, limit sign-ups to your team; the access list is checked either way.

Clerk holds the Google and Microsoft tokens of the accounts people sign in with, which is how their mail and calendar are read. It can also make the token that signs each person in to Swirls without an API key.

Clerk can also say who holds which role: clerk({ roles: 'metadata' }) reads a user's public metadata, and clerk({ roles: { organization: 'org_...' } }) their role in one organization.

Better Auth#

ts
// src/spool.server.tsimport { betterAuth } from "@swirls/spools/auth/better-auth";auth: betterAuth(),// src/spool.ui.tsimport { betterAuthClient } from "@swirls/spools/auth/better-auth/ui";export const authClients = [betterAuthClient];

Better Auth runs inside your server and keeps its users, sessions and accounts in your own database. People sign in with a link sent to their address, or with Google or Microsoft. There are no passwords. Only someone on the access list is sent a link or gets an account.

Install better-auth, then run bun run crm:gen and bun run db:push to create its four tables (auth_users, auth_sessions, auth_accounts, auth_verifications). PGlite creates them itself.

Variable
BETTER_AUTH_SECRET Required in production: 32 or more characters (openssl rand -base64 32). It signs session cookies and encrypts stored Google and Microsoft tokens. Changing it signs everyone out, and people reconnect their accounts
APP_URL Required in production: the app's public address. People must open the app there, or signing in is refused. Cookies are marked secure when it is https
GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET Adds Google. Redirect URI: <APP_URL>/api/auth/callback/google
MICROSOFT_CLIENT_ID, MICROSOFT_CLIENT_SECRET Adds Microsoft. Redirect URI: <APP_URL>/api/auth/callback/microsoft
MICROSOFT_TENANT_ID The one organization whose people may sign in. See below
TRUST_PROXY How many proxies are in front of the server, so that rate limits count the right visitor

In development all of them may be missing: the address is http://localhost:3000, and everyone is signed out when the server restarts.

Links by email need an email provider in production (RESEND_API_KEY and EMAIL_FROM, or another email adapter). In development the link is written to the log. A link works once, for ten minutes. Opening it shows a page with one button, because mail filters open links to check them and would otherwise use it up. The answer to asking for a link is the same, and takes the same time, whether or not the address is on the access list.

Microsoft lets any organization put any address in its own people's profiles, so an address counts only when Microsoft says who owns it. Either set MICROSOFT_TENANT_ID to your own organization, whose word is then taken, or add the xms_edov or verified_primary_email optional claim to the ID token in the app's registration. Without one or the other, signing in with Microsoft always fails.

Mail and calendar. "Open profile" in the mailbox panel asks Google or Microsoft for the mailbox scopes. The account must have the address the person signs in with. If you use the Integrations page too, keep its redirect URIs (<APP_URL>/api/integrations/<provider>/callback) on the same client.

Swirls needs SWIRLS_API_KEY, because Better Auth gives no identity token that Swirls could check.

Removing someone. Take them off the access list. Their row in auth_users stays and opens nothing. Sessions last thirty days from signing in, and signing out takes effect at once.

Status: tested against stand-ins for Google and Microsoft, and by hand in a browser with links by email. It has not been run against live Google or Microsoft accounts.

The development sign-in#

devAuth() signs every request in as one person: whoever DEV_AUTH_EMAIL names, or the first address the access list lets in. DEV_AUTH_SIGNED_OUT=1 shows the app signed out. A new Spool uses it until you set a real provider's keys. It refuses to run with NODE_ENV=production, and a production build leaves it out.

Record scopes#

With scopes in the config, something has to say which scopes each person holds.

  • Clerk organizations. clerk({ scopes: 'organizations', scopesFrom: ['org_...'] }): a person holds the scope of each organization named in scopesFrom that they belong to, with their role in it. The scope is the organization's id (so org_2abc... is what a record's scope field holds; for a select field, make each option's value an organization's id and its label the name people know it by) and the role is the membership's without Clerk's prefix: org:member is member in scopes.roles. The organization active in the session is the scope the person works in; switching with Clerk's own switcher switches the CRM. Every membership and every organization is read, however many there are. Not run against a live Clerk account yet: the mapping and the paging are tested against a stand-in for Clerk's API.

    Say which organizations are scopes, and where head office is. In Clerk, unless you have turned it off, anyone can create an organization. Whoever does is its admin, can give anyone any role in it, and Clerk's built-in org:admin is held by whoever runs any organization at all. An organization like that holds no records, but if it counted as a scope its maker would "write a scope", and whoever writes a scope may change what is shared: shared objects' records, shared views, the file library, Marketing, the team's connections. So nothing is taken on an organization's word. You name the organizations that are scopes, and the ones a role marked all counts from, by id:

    ts
    auth: clerk({  scopes: 'organizations',  scopesFrom: ['org_2abcNorth', 'org_2abcSouth'],  everyScopeFrom: ['org_2abcHeadOffice'],}),

    A membership in an organization that is not in scopesFrom gives nothing, whatever its role, and that organization is never listed as a scope or accepted as a record's. Without scopesFrom no organization is a scope and nobody holds one, and the server says so when it starts ([env] Which Clerk organizations are record scopes: no organization is a scope ...). A role marked all held anywhere but an organization in everyScopeFrom gives nothing either: not every scope, and not that organization's own. Without everyScopeFrom nobody reaches every scope, and the server says that too. Head office's organization is a scope of its own only if it is in scopesFrom as well.

    Adding a branch is adding its organization's id to scopesFrom and deploying. Turning organization creation by users off in the Clerk dashboard is still worth doing, so the list of organizations stays yours, but the CRM does not depend on it.

    Why the id and not the slug. A slug reads better in a config, but it is not Clerk's to keep: an organization's administrators can rename it, and a slug let go of (by a rename, or by deleting the organization) can be taken by another organization. Scopes keyed by slug would hand one group's records to whoever took its slug. An id is never given out twice. If you already hold records whose scope field has slugs in it, change them to the ids before you turn this on.

  • Development. devAuth({ scopes: { list, grants } }), by email address:

    ts
    auth: devAuth({  scopes: {    list: [{ key: 'north', label: 'North' }, { key: 'south', label: 'South' }],    grants: { 'dev@example.com': [{ scope: 'north', role: 'member' }] },  },}),
  • Better Auth, or anything else. A resolver of your own, in src/app/extensions.ts.

What a person holds is kept for a minute, as the access list is, so the provider is not asked on every click. Taking a scope away from someone takes effect within that minute. Switching the scope you work in is not kept: it applies to the next request.

Tokens from your own front end#

A front end of your own for people who are not teammates forwards each person's sign-in token (Building your own door). Checking it is an optional capability of an auth adapter (tokens on the adapter, the Tokens port in packages/spools/src/server/runtime/tokens.ts), behind verifyToken(token, { authorizedParties }) in @swirls/spools/extensions: the user id it was issued to, or null, never a failure. Without it every token is null.

Adapter Tokens
clerk() A session token of the same Clerk instance, issued to one of the origins you name (azp), checked with Clerk's authenticateRequest on a request that carries only the bearer header. CLERK_JWT_KEY lets it check offline; without it a token naming a key the instance doesn't have is refused before Clerk is asked, and the keys are fetched at most once a minute
devAuth() The tokens of outsiders: [{ id, name, email, token }], people the directory knows who are never on the team list. Origins are not checked
betterAuth() None. Its session tokens are the session cookie's value, with no audience: a teammate's token from the CRM itself would count, and nothing ties one to your front end. verifyToken gets null for every token

A session cookie sent with such a request counts for nothing: only the bearer token is checked.

Your own#

An auth adapter has a server half (a directory of people, the session of a request, and optionally middleware and endpoints under /api/auth) and a browser half (the sign-in form, the account button, signing out). Both contracts are short: packages/spools/src/server/runtime/auth.ts and packages/spools/src/crm/ui/auth.tsx.

The one rule that matters is that AuthUser.email is an address the provider has verified, or null. The access list trusts it.

Changing provider#

Records keep the user ids of their owners and of whoever made them, and the ids are the provider's. Moving to another provider gives everyone a new id, so owners, assignees, favourites and "created by" won't match until they are mapped across. Plan that before you switch a Spool people use.