Replaces @clerk/react-router with self-hosted better-auth to eliminate
the external Clerk dependency and keep all user/session data in our own
PostgreSQL database.
**What changed**
- New: auth.server.ts (BetterAuth config w/ Drizzle adapter, bcrypt, Resend), auth-client.ts, api.auth.$.ts handler
- New: /login and /register pages with email+password and Google/Discord OAuth; open-redirect guard on redirectTo param
- New: UserMenu component replacing Clerk's UserButton
- Schema: sessions, accounts, verifications tables; emailVerified column; clerkId made nullable
- Migrations 0081 (BetterAuth tables) and 0082 (accounts extra columns for v1.6.9)
- All ~30 route files: getAuth → auth.api.getSession, isUserAdminByClerkId → isUserAdmin
- root.tsx: isAdmin read directly from session.user.isAdmin (no extra DB query)
- useDraftAuthRecovery: removed Clerk JWT refresh logic; replaced with cookie-session check
- models/user.ts: removed findUserByClerkId, findOrCreateUser, updateUserByClerkId (webhook pattern)
- Deleted: app/routes/api/webhooks/clerk.ts; uninstalled @clerk/react-router, @clerk/themes, svix
- scripts/migrate.mjs: extended with idempotent Clerk → BetterAuth data migration (FK conversion, email_verified, OAuth accounts)
- scripts/migrate-clerk-passwords.mjs: one-time script to import bcrypt hashes from Clerk CSV export
- BETTERAUTH_MIGRATION.md: dev and production runbooks
- All test mocks updated: vi.mock('~/lib/auth.server') instead of @clerk/react-router/server
- Test fixtures: added emailVerified field
**Follow-up (post-stable)**
- Rename actor_clerk_id column → actor_user_id in commissioner_audit_log
- Drop clerk_id column from users once migration confirmed
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import { eq, inArray } from "drizzle-orm";
|
|
import { database } from "~/database/context";
|
|
import * as schema from "~/database/schema";
|
|
|
|
export type User = typeof schema.users.$inferSelect;
|
|
export type NewUser = typeof schema.users.$inferInsert;
|
|
|
|
export function getUserDisplayName(
|
|
user: Pick<User, "username" | "displayName">
|
|
): string | null {
|
|
return user.username ?? user.displayName ?? null;
|
|
}
|
|
|
|
export async function createUser(data: NewUser): Promise<User> {
|
|
const db = database();
|
|
const [user] = await db.insert(schema.users).values(data).returning();
|
|
return user;
|
|
}
|
|
|
|
export async function findUserById(id: string): Promise<User | undefined> {
|
|
const db = database();
|
|
return await db.query.users.findFirst({
|
|
where: eq(schema.users.id, id),
|
|
});
|
|
}
|
|
|
|
export async function findUsersByIds(ids: string[]): Promise<User[]> {
|
|
if (ids.length === 0) return [];
|
|
const db = database();
|
|
return await db
|
|
.select()
|
|
.from(schema.users)
|
|
.where(inArray(schema.users.id, ids));
|
|
}
|
|
|
|
export async function updateUser(
|
|
id: string,
|
|
data: Partial<NewUser>
|
|
): Promise<User> {
|
|
const db = database();
|
|
const [user] = await db
|
|
.update(schema.users)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(schema.users.id, id))
|
|
.returning();
|
|
return user;
|
|
}
|
|
|
|
export async function deleteUser(id: string): Promise<void> {
|
|
const db = database();
|
|
await db.delete(schema.users).where(eq(schema.users.id, id));
|
|
}
|
|
|
|
export async function findAdmins(): Promise<User[]> {
|
|
const db = database();
|
|
return await db.query.users.findMany({
|
|
where: eq(schema.users.isAdmin, true),
|
|
orderBy: (users, { asc }) => [asc(users.displayName)],
|
|
});
|
|
}
|
|
|
|
export async function isUserAdmin(userId: string): Promise<boolean> {
|
|
const user = await findUserById(userId);
|
|
return user?.isAdmin ?? false;
|
|
}
|
|
|
|
export async function setUserAdmin(userId: string, isAdmin: boolean): Promise<User> {
|
|
return await updateUser(userId, { isAdmin });
|
|
}
|
|
|
|
export async function findAllUsers(): Promise<User[]> {
|
|
const db = database();
|
|
return await db.query.users.findMany({
|
|
orderBy: (users, { asc }) => [asc(users.displayName)],
|
|
});
|
|
}
|