migrate: replace NextAuth.js with Better Auth

- Install better-auth and @better-auth/drizzle-adapter
- Create src/lib/auth.ts with Better Auth configuration using bcrypt
- Update database schema: change auth table IDs from uuid to text
- Update route handler from /api/auth/[...nextauth] to /api/auth/[...all]
- Update tRPC context and middleware for Better Auth session handling
- Update client components to use Better Auth APIs (signIn, signOut)
- Update seed script with text-based IDs and correct account schema
- Fix type errors in wizard components (robotId, optional chaining)
- Fix API paths: api.robots.initialize -> api.robots.plugins.initialize
- Update auth router to use text IDs for Better Auth compatibility

Note: Auth tables were reset - users will need to re-register.
This commit is contained in:
Sean O'Connor
2026-03-21 23:03:55 -04:00
parent 4bed537943
commit 20d6d3de1a
37 changed files with 460 additions and 1182 deletions

View File

@@ -1,5 +1,5 @@
import { redirect } from "next/navigation";
import { cookies } from "next/headers";
import { cookies, headers } from "next/headers";
import {
SidebarInset,
SidebarProvider,
@@ -7,7 +7,7 @@ import {
} from "~/components/ui/sidebar";
import { Separator } from "~/components/ui/separator";
import { AppSidebar } from "~/components/dashboard/app-sidebar";
import { auth } from "~/server/auth";
import { auth } from "~/lib/auth";
import {
BreadcrumbProvider,
BreadcrumbDisplay,
@@ -22,16 +22,15 @@ interface DashboardLayoutProps {
export default async function DashboardLayout({
children,
}: DashboardLayoutProps) {
const session = await auth();
const session = await auth.api.getSession({
headers: await headers(),
});
if (!session?.user) {
redirect("/auth/signin");
}
const userRole =
typeof session.user.roles?.[0] === "string"
? session.user.roles[0]
: (session.user.roles?.[0]?.role ?? "observer");
const userRole = "researcher"; // Default role for dashboard access
const cookieStore = await cookies();
const defaultOpen = cookieStore.get("sidebar_state")?.value === "true";