Files
hristudio/src/lib/auth.ts
Sean O'Connor 20d6d3de1a 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.
2026-03-21 23:03:55 -04:00

80 lines
1.7 KiB
TypeScript

import { betterAuth } from "better-auth";
import { drizzleAdapter } from "@better-auth/drizzle-adapter";
import { nextCookies } from "better-auth/next-js";
import { db } from "~/server/db";
import {
users,
accounts,
sessions,
verificationTokens,
} from "~/server/db/schema";
import bcrypt from "bcryptjs";
const baseURL =
process.env.NEXTAUTH_URL ||
process.env.BETTER_AUTH_URL ||
"http://localhost:3000";
export const auth = betterAuth({
baseURL,
database: drizzleAdapter(db, {
provider: "pg",
schema: {
user: users,
account: accounts,
session: sessions,
verification: verificationTokens,
},
}),
emailAndPassword: {
enabled: true,
password: {
hash: async (password: string) => {
return bcrypt.hash(password, 12);
},
verify: async ({
hash,
password,
}: {
hash: string;
password: string;
}) => {
return bcrypt.compare(password, hash);
},
},
},
session: {
expiresIn: 60 * 60 * 24 * 30,
updateAge: 60 * 60 * 24,
modelName: "session",
fields: {
id: "id",
token: "token",
userId: "userId",
expiresAt: "expiresAt",
ipAddress: "ipAddress",
userAgent: "userAgent",
},
},
account: {
modelName: "account",
fields: {
id: "id",
providerId: "providerId",
accountId: "accountId",
userId: "userId",
accessToken: "accessToken",
refreshToken: "refreshToken",
expiresAt: "expiresAt",
scope: "scope",
},
},
pages: {
signIn: "/auth/signin",
error: "/auth/error",
},
plugins: [nextCookies()],
});
export type Session = typeof auth.$Infer.Session;