import { betterAuth } from "better-auth"; import { drizzleAdapter } from "better-auth/adapters/drizzle"; import { headers } from "next/headers"; import * as schema from "@/db/schema"; import { db } from "@/lib/db"; import { sendEmail } from "@/lib/email"; import { createSetting } from "./setting"; export const auth = betterAuth({ database: drizzleAdapter(db, { provider: "pg", schema, }), user: { changeEmail: { enabled: true } }, emailVerification: { sendVerificationEmail: async ({ user, url, token }) => { await sendEmail({ to: user.email, subject: "Confirm your email - Vouch", html: `
Please confirm your email address by clicking the link below:
If you did not request this, you can ignore this email.
`, }); }, }, socialProviders: { google: { clientId: process.env.GOOGLE_CLIENT_ID as string, clientSecret: process.env.GOOGLE_CLIENT_SECRET as string, }, }, databaseHooks: { user: { create: { after: async (user) => { await createSetting(user.id); }, }, }, }, }); export async function getUser() { const data = await auth.api.getSession({ headers: await headers() }); return data?.user || null; } export async function getUserId() { const user = await getUser(); return user?.id || null; }