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>
106 lines
3.5 KiB
TypeScript
106 lines
3.5 KiB
TypeScript
import { redirect, Form } from "react-router";
|
|
import { auth } from "~/lib/auth.server";
|
|
import { findUserById, updateUser } from "~/models/user";
|
|
import type { Route } from "./+types/user-profile";
|
|
import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card";
|
|
import { Button } from "~/components/ui/button";
|
|
import { Input } from "~/components/ui/input";
|
|
import { Label } from "~/components/ui/label";
|
|
|
|
export function meta(): Route.MetaDescriptors {
|
|
return [{ title: "Profile - Brackt" }];
|
|
}
|
|
|
|
export async function loader(args: Route.LoaderArgs) {
|
|
const session = await auth.api.getSession({ headers: args.request.headers });
|
|
if (!session) {
|
|
return redirect("/login?redirectTo=/user-profile");
|
|
}
|
|
const user = await findUserById(session.user.id);
|
|
if (!user) {
|
|
return redirect("/");
|
|
}
|
|
return { user };
|
|
}
|
|
|
|
export async function action(args: Route.ActionArgs) {
|
|
const session = await auth.api.getSession({ headers: args.request.headers });
|
|
if (!session) {
|
|
return redirect("/login?redirectTo=/user-profile");
|
|
}
|
|
|
|
const formData = await args.request.formData();
|
|
const displayName = formData.get("displayName") as string;
|
|
const username = formData.get("username") as string | null;
|
|
const firstName = formData.get("firstName") as string | null;
|
|
const lastName = formData.get("lastName") as string | null;
|
|
|
|
await updateUser(session.user.id, {
|
|
displayName: displayName || undefined,
|
|
username: username || undefined,
|
|
firstName: firstName || undefined,
|
|
lastName: lastName || undefined,
|
|
});
|
|
|
|
return { success: true };
|
|
}
|
|
|
|
export default function UserProfilePage({ loaderData, actionData }: Route.ComponentProps) {
|
|
const { user } = loaderData;
|
|
|
|
return (
|
|
<div className="container mx-auto py-8 px-4 max-w-lg">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Your Profile</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
{actionData?.success && (
|
|
<p className="text-sm text-green-500 mb-4">Profile updated successfully.</p>
|
|
)}
|
|
<Form method="post" className="space-y-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="displayName">Display Name</Label>
|
|
<Input
|
|
id="displayName"
|
|
name="displayName"
|
|
defaultValue={user.displayName ?? ""}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="username">Username</Label>
|
|
<Input
|
|
id="username"
|
|
name="username"
|
|
defaultValue={user.username ?? ""}
|
|
/>
|
|
</div>
|
|
<div className="grid grid-cols-2 gap-4">
|
|
<div className="space-y-2">
|
|
<Label htmlFor="firstName">First Name</Label>
|
|
<Input
|
|
id="firstName"
|
|
name="firstName"
|
|
defaultValue={user.firstName ?? ""}
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="lastName">Last Name</Label>
|
|
<Input
|
|
id="lastName"
|
|
name="lastName"
|
|
defaultValue={user.lastName ?? ""}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="space-y-1">
|
|
<Label>Email</Label>
|
|
<p className="text-sm text-muted-foreground">{user.email}</p>
|
|
</div>
|
|
<Button type="submit" className="w-full">Save Changes</Button>
|
|
</Form>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
);
|
|
}
|