Files
petvity/lib/auth/shared/runtime.ts
T
CatoandClaude 2d3609c13e feat(auth): stage AccountKit beside Auth.js, switch off (#156)
Petvity moves from Auth.js to @bitbaum/accountkit one reversible step at a
time, as evig and kivvi do. Nothing changes while AUTH_RUNTIME is unset.

- Migration 0008: AccountKit's tables under an `auth_` prefix, plus the
  users <-> auth_users mirror functions, inert until attached.
- scripts/db/accountkit-import.sql: accounts under their own ids, bcrypt
  hashes byte for byte, verification kept, orangecat_sub as an `orangecat`
  account row; refuses collisions, drift or a live shared runtime, then
  attaches the triggers. The rehearsal CLI always rolls back.
- lib/auth/shared/: the runtime (ids: 'uuid', legacy bcrypt, AccountKit
  0.16.0), a session reader re-read from `users` on every request, a bridge
  that answers next-auth/react including "Sign in with OrangeCat", and a
  Petvity row for a person AccountKit creates itself. AccountKit's own
  credential endpoints stay closed; Petvity's writers stay the only ones.
- lib/auth/claims.ts: the ADMIN_EMAILS promotion rule, written once.
- The proxy has one routing function and two session sources.
- browser-smoke.yml runs the account journey once per runtime; the
  AccountKit leg first runs the rehearsal and the real-database journeys.

docs/ACCOUNTKIT-MIGRATION.md records what is left before switching.


Claude-Session: https://claude.ai/code/session_019MDtmud6zcK4rStBGmYSML

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-01 08:31:55 +01:00

41 lines
1.7 KiB
TypeScript

/**
* Petvity on AccountKit's shared account runtime. Off until
* AUTH_RUNTIME=accountkit; Auth.js stays authoritative until the import and the
* switch (docs/ACCOUNTKIT-MIGRATION.md).
*/
import bcrypt from "bcryptjs";
import { createAuthRuntime } from "@bitbaum/accountkit/auth/next";
import { createAuthMailer } from "@bitbaum/accountkit/auth/mail";
import { APP_URL } from "@/lib/config/app";
import { getInstance } from "@/lib/db";
import { authTables } from "@/lib/db/accountkit";
import { ensurePetvityUser } from "./accounts";
/** Whether the shared runtime answers. Auth.js answers otherwise. */
export function sharedAuthEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
return env.AUTH_RUNTIME === "accountkit";
}
/** The public origin every redirect and every AccountKit origin check uses. */
export function siteUrl(env: NodeJS.ProcessEnv = process.env): string {
return (env.AUTH_URL || APP_URL).replace(/\/$/, "");
}
const site = { url: siteUrl(), title: "Petvity", slug: "petvity" };
// The secret is BETTER_AUTH_SECRET, or the AUTH_SECRET the box already holds.
export const sharedAuth = createAuthRuntime({
site,
// Account mail through the fleet transport (RESEND_*), as Petvity's own is.
mail: createAuthMailer({ site }),
isDatabaseConfigured: (env) => sharedAuthEnabled(env) && Boolean(env.DATABASE_URL),
getDatabase: () => getInstance(),
schema: authTables,
// Imported bcrypt hashes (users.password) keep working, and Petvity's own
// writers (registration, reset, password change) keep writing bcrypt.
legacyBcrypt: bcrypt,
// users.id is a uuid that every Petvity table references.
ids: "uuid",
onAccountCreated: ensurePetvityUser,
});