this repo has no description
1import { betterAuth } from "better-auth";
2import { drizzleAdapter } from "better-auth/adapters/drizzle";
3import { headers } from "next/headers";
4import * as schema from "@/db/schema";
5import { db } from "@/lib/db";
6import { sendEmail } from "@/lib/email";
7import { createSetting } from "./setting";
8
9export const auth = betterAuth({
10 database: drizzleAdapter(db, {
11 provider: "pg",
12 schema,
13 }),
14 user: { changeEmail: { enabled: true } },
15 emailVerification: {
16 sendVerificationEmail: async ({ user, url, token }) => {
17 await sendEmail({
18 to: user.email,
19 subject: "Confirm your email - Vouch",
20 html: `<p>Please confirm your email address by clicking the link below:</p>
21 <p><a href="${url}">${url}</a></p>
22 <p>If you did not request this, you can ignore this email.</p>`,
23 });
24 },
25 },
26 socialProviders: {
27 google: {
28 clientId: process.env.GOOGLE_CLIENT_ID as string,
29 clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
30 },
31 },
32 databaseHooks: {
33 user: {
34 create: {
35 after: async (user) => {
36 await createSetting(user.id);
37 },
38 },
39 },
40 },
41});
42
43export async function getUser() {
44 const data = await auth.api.getSession({ headers: await headers() });
45 return data?.user || null;
46}
47
48export async function getUserId() {
49 const user = await getUser();
50 return user?.id || null;
51}