mirror of
https://github.com/soconnor0919/hristudio.git
synced 2026-03-23 19:27:51 -04:00
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:
79
src/lib/auth.ts
Normal file
79
src/lib/auth.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user