mirror of
https://github.com/bitbaum/petvity.git
synced 2026-12-04 22:27:16 +01:00
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>
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
/**
|
|
* Petvity's session shape, answered from a shared AccountKit session. Every
|
|
* request rereads the `users` row, so a role change, a renamed account, a newly
|
|
* verified address or a deleted account applies at once — `update()` has
|
|
* nothing left to carry.
|
|
*/
|
|
import * as React from "react";
|
|
import { eq } from "drizzle-orm";
|
|
import type { Session } from "next-auth";
|
|
import { getInstance } from "@/lib/db";
|
|
import { users } from "@/lib/db/schema";
|
|
import { sharedAuth } from "./runtime";
|
|
|
|
type SharedSession = { user: { id: string }; session: { expiresAt: Date | string } };
|
|
|
|
export async function petvitySession(shared: SharedSession | null): Promise<Session | null> {
|
|
if (!shared) return null;
|
|
const row = await getInstance().query.users.findFirst({ where: eq(users.id, shared.user.id) });
|
|
if (!row) return null;
|
|
return {
|
|
user: {
|
|
id: row.id,
|
|
name: row.name,
|
|
email: row.email,
|
|
image: row.image,
|
|
role: row.role,
|
|
emailVerified: row.emailVerified,
|
|
},
|
|
expires: new Date(shared.session.expiresAt).toISOString(),
|
|
};
|
|
}
|
|
|
|
// React.cache dedupes per request on the server; outside React (tests) it is absent.
|
|
const cache: <T extends (...args: never[]) => unknown>(fn: T) => T = React.cache ?? ((fn) => fn);
|
|
|
|
/** The session of this request (server components, route handlers). */
|
|
export const sharedSession = cache(async () => petvitySession(await sharedAuth.getSession()));
|
|
|
|
/** The session behind request headers (the proxy, the /session endpoint). */
|
|
export async function sharedSessionFor(headers: Headers): Promise<Session | null> {
|
|
return petvitySession(await sharedAuth.sessionFor(headers).catch(() => null));
|
|
}
|
|
|
|
/** The shared session id behind these headers, for "keep this session" writes. */
|
|
export async function sharedSessionId(headers: Headers): Promise<string | null> {
|
|
const shared = await sharedAuth.sessionFor(headers).catch(() => null);
|
|
return shared?.session.id ?? null;
|
|
}
|