feat: migrate to Better Auth for authentication, update environment variables, and enhance database schema with accounts and sessions
This commit is contained in:
32
Backend/auth.ts
Normal file
32
Backend/auth.ts
Normal 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>>;
|
||||
Reference in New Issue
Block a user