Auth and organizations

Authentication is Better Auth, configured once in packages/auth and used by the API, web and mobile.

What's included

  • Email and password sign-up and sign-in, email verification, password reset
  • Organizations with roles owner, admin, member
  • Invitations by email (owners and admins), accept or decline on the web
  • Platform admins (user.role = "admin"): see Admin
  • Sessions on mobile stored in SecureStore (@better-auth/expo)

Active organization

Every session has an activeOrganizationId. New sessions start in the user's first organization; switching happens with organization.setActive({ organizationId }) (the sidebar's switcher on web, the chips on mobile).

On the API, read it with activeOrganizationId(c) and filter every query by it:

const orgId = activeOrganizationId(c);
const rows = await db.select().from(schema.project).where(eq(schema.project.organizationId, orgId));

Protect routes with the middleware from apps/api/src/middleware.ts:

new Hono<AppEnv>().use(requireUser, requireOrganization).get("/", ...)

On the client

// web: apps/web/src/lib/auth-client.ts   mobile: apps/mobile/src/lib/auth-client.ts
const { data: session } = useSession();
const { data: org } = useActiveOrganization();        // includes members and invitations
await organization.inviteMember({ email, role: "member" });

The server enforces permissions: a member who calls inviteMember gets a 403.

Changing auth

Add or configure Better Auth plugins in packages/auth/src/index.ts (and their client plugins in packages/auth/src/client.ts), then regenerate the tables and create a migration:

pnpm auth:generate
pnpm db:generate
pnpm db:migrate