BetterAuth — Authentication Layer¶
Centralized authentication for all Bricks apps (investor mobile, investor web, project-owner portal, admin backoffice, notary backoffice).
Dual-instance architecture (May 2026)¶
Two BetterAuth instances are built from the same factory, sharing the same Postgres tables and secret but isolated by route path and cookie prefix. This allows a browser to hold an admin backoffice session and a client session simultaneously without cookie collisions.
The notary app has two doors on one login page. Notary staff use clientAuth (same cookie as invest/PDP) plus an active notary_user row. Bricks admins use adminAuth (role = 'admin'). The guard does not write a notary_user row for them. Not a third Better Auth instance.
clientAuth |
adminAuth |
|
|---|---|---|
| Purpose | All client apps (investor mobile/web + project-owner portal + notary) | Admin backoffice only |
| Route base path | /api/auth/* |
/api/auth/admin/* |
| Cookie prefix | better-auth (default, kept for backward compat) |
bricks-admin |
| CORS / trusted origins | CORS_CLIENT_URLS + native schemes + Apple SSO (include the notary origin here) |
CORS_ADMIN_CLIENT_URLS |
| OAuth providers | Google, Facebook, LinkedIn, Apple | Google only |
| Email/password | Enabled | Disabled (Google OAuth only) |
| Plugins | expo(), twoFactor, emailOTP, admin(), lastLoginMethod() |
admin(), lastLoginMethod() |
| Session guard | JwtAuthGuard (investors) / NotaryAuthGuard (notary routes: client session with an active notary_user, or admin session) |
AdminAuthGuard |
Notary staff accounts are created by scripts/notary/manage-notary-access.script.ts. A client session alone is not enough: the guard requires an active notary_user row. An admin session (role = 'admin') is enough on its own and does not create that row. request.user.id is the Better Auth user id (admin_action.adminId and processed.by).
src/lib/better-auth/
├── better-auth.ts Entry point — exports clientAuth + adminAuth
├── better-auth.factory.ts Parameterized factory (cookiePrefix, basePath, plugins à la carte)
├── better-auth.constants.ts Cookie prefixes, impersonation TTL
├── better-auth.schema.ts Zod schemas shared by the factory and hooks (`registeredFrom`)
├── better-auth.utils.ts Header conversion, cookie relay helpers
├── better-auth.repository.ts DB queries (legacy password rehash, customer linkage)
├── better-auth-fastify.module.ts NestJS module that mounts both instances on Fastify
├── client-auth-impersonation.service.ts Admin→client impersonation bridge
├── generate-apple-client-secret.ts Apple SSO JWT client secret (ES256, 6-month TTL)
├── hooks/
│ ├── oauth-error-redirect.hook.ts Rewrites OAuth error redirects to correct client
│ └── registered-from-oauth.hook.ts Copies `registeredFrom` from the OAuth state on social sign-up
├── should-use-cross-origin-cookies.ts Cross-origin cookie detection logic
Auth guards¶
| Guard | Protects | Module |
|---|---|---|
JwtAuthGuard + UserRoleGuard |
Investor endpoints | src/__new/modules/customer-auth/ |
AdminAuthGuard |
Admin endpoints | src/__new/modules/admin-auth/ |
NotaryAuthGuard |
Notary endpoints | src/__new/modules/notary-auth/ |
Admin auth (BetterAuth-only, May 2026)¶
Admin authentication was fully migrated to BetterAuth cookie sessions in May 2026 (#5057). The legacy admins table was dropped.
How it works:
- Admins are rows in better_auth_user with role = 'admin'
- AdminAuthGuard calls auth.api.getSession({ headers }) and checks user.role === 'admin'
- No JWT fallback — exclusively cookie-based
- The admin_legacy_id_mapping table preserves the old admins.id → better_auth_user.id mapping for cross-referencing in external dashboards
Customer auth (Better Auth sessions)¶
Investor REST endpoints require a valid Better Auth session cookie on clientAuth (/api/auth/*). Native apps persist the cookie via @better-auth/expo; web clients use credentials: 'include'.
MCP OAuth (clientAuth only)¶
/mcp is the only route that takes a bearer token instead of the cookie. clientAuth acts as the OAuth 2.1 authorization server for MCP clients (Claude): dynamic client registration, PKCE, refresh tokens. The access token is a JWT whose audience is the /mcp URL (RFC 8707 resource).
McpOAuthGuardverifies the JWT againstclientAuthJWKS, resolves thecustomersrow fromsub, and checks theENABLE_MCP_CONNECTORfeature flag (off, open to thetechgroup) andcustomers.aiAssistantsAllowed(default true). No token → 401 with the RFC 9728WWW-Authenticatechallenge; valid token but unknown user, flag off, or assistants disabled → 403. The column is what refuses an already-issued JWT.POST /oauth2/consentwith the switch off is turned into a refusal: the client is sent back to itsredirect_uriwithaccess_denied- Discovery:
/.well-known/oauth-protected-resource/mcpand/.well-known/oauth-authorization-server/api/auth(mcp-oauth-discovery.controller.ts) - Login and consent happen on the investor web app (
/login,/oauth-consent), desktop only: both paths are excluded from the universal links / App Links, since the native app has no OAuth provider client. An existing session cookie skips the login. The consent screen shows the client name and the redirect host better-auth.oauth-provider.tsregisters anonymous dynamic clients as public (token_endpoint_auth_method: 'none'): 1.6.2 rejects Claude's confidential registration without a session- Refresh tokens last 7 days: the refresh grant ignores logout and session revocation
Social providers¶
| Provider | Notes |
|---|---|
| Standard OAuth2 | |
| Standard OAuth2 | |
| Standard OAuth2 | |
| Apple | Uses form_post response mode; clientSecret is a dynamically-generated ES256 JWT (6-month TTL). appleid.apple.com must be in trustedOrigins because Apple POST-submits back to the callback |
OAuth error handling¶
Two hooks intercept failed OAuth flows:
-
Before hook (
createOAuthOriginCookieHook): On/sign-in/social, captures thecallbackURLorigin into abricks_oauth_origincookie (validated against trusted origins). Purpose: know where to redirect on error. -
After hook (
createOAuthErrorRedirectHook): Intercepts BetterAuth's default error redirect (/error?error=...or/callback/:provider?error=...) and rewrites: - Web: redirects to
${cookieOrigin}/login?error=social-auth - Native: cookie is absent (Safari/ASWebAuthenticationSession jar isolation) — redirects to
${scheme}://login?error=social-authwhere scheme isbricks(prod) orbricksdev(other envs)
State mismatch mitigation¶
skipStateCookieCheck: true disables BetterAuth's state-cookie equality check. CSRF protection is maintained via the DB verification row (single-use, 10-min TTL, PKCE codeVerifier bound). This avoids false state_security_mismatch failures from cookie propagation issues on native.
Admin impersonation (May 2026)¶
"Login as customer" from the backoffice. The admin session (bricks-admin.* cookie) triggers impersonation on clientAuth, producing a separate client session (better-auth.* cookie) for the target customer.
Flow:
1. Admin hits GET /administration/customers/:id/admin-login (protected by AdminAuthGuard)
2. ClientAuthImpersonationService relays the admin session cookie from bricks-admin.* → better-auth.* prefix (same signed token, same DB row)
3. Calls clientAuth.api.impersonateUser({ userId: target.betterAuthUserId })
4. Response forwards client-prefix Set-Cookie headers + { url: frontUrl } for redirect
5. Impersonation session expires after 10 minutes (ADMIN_IMPERSONATE_SESSION_DURATION_SEC)
The admin's backoffice session (bricks-admin.*) remains untouched — both sessions coexist in the same browser.
Two consumers, one trunk. Project owners are on the same clientAuth instance as investors, so
GET /administration/project-financing-request/users/:projectUserId/admin-login (BO Projects) reuses
ClientAuthImpersonationService.impersonateUserAsAdmin — only the redirect front differs
(pdpFrontUrl vs frontUrl). Both refuse an admin target: admin({ adminRoles: ['admin'] }) in
better-auth.factory.ts marks role = 'admin' as admin, and the plugin answers 403
(YOU_CANNOT_IMPERSONATE_ADMINS), mapped to impersonation-forbidden. Never grant the
impersonate-admins permission nor set allowImpersonatingAdmins — that lifts the only guard. The Slack
alert fires once the session exists, so a refused attempt posts nothing.
An impersonated session carries better_auth_session.impersonatedBy, so any "last login" read must
exclude it — an admin login is not the user's.
Plugins¶
| Plugin | Purpose | Instance |
|---|---|---|
expo() |
Rewrites expo-origin header to origin so native requests pass CSRF validation |
clientAuth only |
admin() |
Adds role field to users, enables admin-specific session checks and impersonateUser |
Both |
lastLoginMethod() |
Tracks last auth method (email / OAuth provider) — cookie + better_auth_user.lastLoginMethod |
Both |
twoFactor() |
TOTP-based 2FA with OTP delivered via Customer.io transactional email | clientAuth only |
emailOTP() |
6-digit email verification code (300s TTL), delivered via Customer.io | clientAuth only |
jwt() |
Signs MCP OAuth access tokens and serves the JWKS. disableSettingJwtHeader keeps /get-session from signing a JWT |
clientAuth only |
oauthProvider() |
OAuth 2.1 authorization server for MCP clients (see MCP OAuth) | clientAuth only |
lastLoginMethod uses storeInDatabase: true (column on better_auth_user, applied by the Better Auth migrate CI step — and by pnpm better-auth:check-and-migrate --apply in the integration global-setup, since that harness is otherwise Flyway-only).
Pre-login badge (investor app): the plugin cookie is API-host-scoped (not readable from the front). After a successful login the client writes the known method to local storage (useLogin / native social / 2FA). Web social stashes the provider in sessionStorage before redirect and SessionGateProvider applies it only once a session exists — pending is cleared on login screen mount, email/2FA start, and OAuth error so an abandoned social click cannot overwrite a later email login.
Database tables¶
| Table | Purpose |
|---|---|
better_auth_user |
Users (investors + admins). Roles: null (investor), 'admin'. Optional lastLoginMethod (email / OAuth provider id) |
better_auth_session |
Active sessions (cookie-based) |
better_auth_account |
Credential + social provider links per user |
better_auth_verification |
OAuth state, email OTP, and 2FA verification tokens |
better_auth_two_factor |
2FA configuration per user |
better_auth_jwks |
Key pairs signing MCP OAuth access tokens |
better_auth_oauth_client |
OAuth clients registered through DCR |
better_auth_oauth_access_token |
Opaque access tokens (unused while every client sends resource) |
better_auth_oauth_refresh_token |
OAuth refresh tokens |
better_auth_oauth_consent |
Scopes granted per user and client |
admin_legacy_id_mapping |
Reference table: old admins.id → better_auth_user.id |
Database hooks¶
databaseHooks.session.create.after is caller-provided (sessionProvisioning option) and wired on clientAuth only, via provisionClientProfiles in better-auth.ts. adminAuth mounts no session hook. provisionClientProfiles skips customer / PDP creation for users with a notary_user row so a notary login does not create those profiles. lastLoginAt still runs (no-op unless a customers row already exists — --grant-existing). Notary accounts are created by SQL in scripts/notary/manage-notary-access.script.ts, not public sign-up/email — registeredFrom is client-writable and must not gate provisioning. The BO users list reads it to show financing sign-ups that hold no project yet — display only.
databaseHooks.user.create.before is wired on every instance. On a social sign-up (/callback/:id), it copies registeredFrom from the OAuth state, where the client put it via signIn.social({ additionalData }).
databaseHooks.user.create.after is caller-provided (userProvisioning option) and wired on clientAuth only. It runs once per account, after its creation commits, so it never throws: the sign-up already succeeded. ProjectFinancingRequestReferralService.setReferrerOnSignUp reads a referral code from a financing sign-up (/sign-up/email body or OAuth state additionalData), then sets the referrer of the new project-financing-request user. An existing account signing in with Google creates no user, so it never gets a referrer.
On a client session creation:
1. findOrCreateCustomerForBetterAuthUser — ensures a customers row exists for the user (first-login scenario). Emits account_created event to Customer.io if new investor.
2. ProjectFinancingRequestUserService.findOrCreate — ensures a project-financing-request owner record exists (for the project-owner portal).
3. updateLastLoginAtByBetterAuthUserId — updates customers.lastLoginAt.
Password handling¶
- Current: Argon2id (timeCost=3, memoryCost=64MB, parallelism=4)
- Legacy: HMAC SHA-256. On successful verify, automatically rehashes to Argon2id (transparent migration)
- Min password length:
MIN_PASSWORD_LENGTH_API(8) on the API validator until all clients enforce 12 chars; mobile/web clients validate withMIN_PASSWORD_LENGTH(12) viapasswordValidator
Password recovery & change (clientAuth, May 2026)¶
Investor mobile and web call Better Auth directly — no legacy POST /customers/forgot-password for new flows.
| Client method | Route (under /api/auth/*) |
Used by |
|---|---|---|
requestPasswordReset |
forgot-password flow (email with reset link) | useRequestPasswordRecovery |
resetPassword |
reset with token from email deep link | useResetPassword |
changePassword |
authenticated password update | useChangePassword |
changePassword accepts revokeOtherSessions: boolean. When true, other better_auth_session rows for the user are invalidated; the calling session remains valid.
Legacy Redis MFA (POST /auth/mfa/*) still exists for some flows (withdraw, email update). Login 2FA uses Better Auth twoFactor plugin — see mobile AUTH_FLOWS.md.
Session listing & revocation¶
| Client method | Purpose |
|---|---|
listSessions |
Returns active sessions (user agent, createdAt, token id) for the Security screen |
revokeSession |
Ends a single session by token; current session cannot be revoked from the UI |
Sessions are rows in better_auth_session, scoped to clientAuth cookie prefix.
TTL:
- clientAuth — optional BETTER_AUTH_SESSION_CLIENT_EXPIRES_IN_SECONDS (e.g. 2592000 = 30 days); omit to keep Better Auth’s 7-day default
- adminAuth — Better Auth default (7 days); shorter on purpose for the critical backoffice surface
Better Auth default updateAge (1 day) still applies: when a session is used after ≥1 day, expiresAt is pushed to now + expiresIn. Each device/login has its own row and clock (desktop vs mobile are independent).
Cookie strategy¶
Cookie names are determined by the instance's cookiePrefix:
| Instance | Session cookie (HTTP) | Session cookie (HTTPS) |
|---|---|---|
clientAuth |
better-auth.session_token |
__Secure-better-auth.session_token |
adminAuth |
bricks-admin.session_token |
__Secure-bricks-admin.session_token |
| Condition | sameSite |
secure |
|---|---|---|
| Cross-origin (API domain ≠ front domain) | none |
true |
| Same-origin (localhost dev) | lax |
false |
Detection via shouldUseCrossOriginCookies(corsClientUrls, baseURL).
Native apps: @better-auth/expo handles cookie persistence in expo-secure-store. Known pitfall: iOS keychain throws KeyChainException when device is locked — the mobile app wraps storage access in a try/catch fallback (#5291).
Trusted origins¶
Browser CORS for the NestJS routes is a separate layer: main.ts feeds buildOriginMatcher CORS_CLIENT_URLS. CORS is not authorization — the guards are.
| Instance | Env var | Extra entries |
|---|---|---|
clientAuth |
CORS_CLIENT_URLS |
bricks://, bricksdev://, https://appleid.apple.com — put the notary origin in this list |
adminAuth |
CORS_ADMIN_CLIENT_URLS |
none (web backoffice only) |
Same comma-separated format. If the list contains * (local dev only), all HTTP/HTTPS origins are trusted for that instance. * is rejected in staging/production.
Guard → instance mapping: AdminAuthGuard must use adminAuth. Investor/customer guards must use clientAuth. NotaryAuthGuard reads adminAuth first (only role = 'admin') and otherwise clientAuth. Neither door creates a notary_user row.
IP address resolution¶
Uses Cloudflare's cf-connecting-ip header (not spoofable) with x-forwarded-for as fallback.
Logging¶
The BetterAuth logger bridge (betterAuthLogger in better-auth.factory.ts) drops credential-flow user errors only (Invalid password, Credential account not found, User not found, …). OAuth/state/callback failures stay at error — they can signal cookie-jar bugs, infra, or front regressions.
Troubleshooting¶
| Symptom | Likely cause |
|---|---|
state_security_mismatch on social login |
Double-tap triggering concurrent OAuth flows (mitigated by 2s throttle on front) |
Apple SSO → UNKNOWN error |
Expired/invalid Apple client secret (regenerated from private key, 6-month TTL) |
INVALID_ORIGIN on Apple callback |
appleid.apple.com not in trustedOrigins |
| 401 on admin endpoints | Session cookie not sent (withCredentials: true missing) or session expired |
| Admin login opens front but user is logged out | Set-Cookie from GET /administration/customers/:id/admin-login not stored — check CORS credentials + API cookie domain |
| iOS app crash on locked device | expo-secure-store keychain access — caught by safe storage wrapper |
INVALID_CALLBACK_URL on Google |
callbackURL contains encoded characters; fixed by proper URL encoding on front |