feat: migrate to Better Auth for authentication, update environment variables, and enhance database schema with accounts and sessions

This commit is contained in:
2025-12-20 11:00:00 +00:00
parent 377836d1fa
commit 7bff6b0f91
13 changed files with 183 additions and 205 deletions

32
Backend/auth.ts Normal file
View File

@@ -0,0 +1,32 @@
import { betterAuth } from 'better-auth';
import { drizzleAdapter } from 'better-auth/adapters/drizzle';
import { db } from './db/client';
import { schema } from './db/schema';
import { hashPassword, verifyPassword } from './utils/password';
const trustedOrigins = process.env.BETTER_AUTH_TRUSTED_ORIGINS
? process.env.BETTER_AUTH_TRUSTED_ORIGINS.split(',').map((origin) => origin.trim()).filter(Boolean)
: undefined;
export const auth = betterAuth({
database: drizzleAdapter(db, {
provider: 'pg',
schema: {
...schema,
user: schema.users,
},
}),
emailAndPassword: {
enabled: true,
password: {
hash: async (password) => hashPassword(password),
verify: async ({ hash, password }) => verifyPassword(password, hash),
},
},
secret: process.env.BETTER_AUTH_SECRET,
baseURL: process.env.BETTER_AUTH_URL,
trustedOrigins,
});
export type AuthSession = Awaited<ReturnType<typeof auth.api.getSession>>;