|
1 | | -import { betterAuth } from "better-auth"; |
2 | | -import { drizzleAdapter } from "better-auth/adapters/drizzle"; |
3 | | -import { apiKey, magicLink, organization } from "better-auth/plugins"; |
4 | | -import { schema } from "@/server/db"; |
| 1 | +import { getAuth } from "@refref/auth"; |
| 2 | +import { db, schema } from "@/server/db"; |
5 | 3 | import { env } from "@/env"; |
6 | | -import { db } from "@/server/db"; |
7 | 4 | import { logger } from "@/lib/logger"; |
8 | | -import { emailService } from "@/lib/email"; |
9 | 5 | import { posthog } from "@/lib/posthog"; |
10 | 6 |
|
11 | | -// Build social providers object dynamically based on enabled providers |
12 | | -const socialProviders = env.NEXT_PUBLIC_ENABLED_SOCIAL_AUTH.reduce( |
13 | | - (acc, provider) => { |
14 | | - if (provider === "google") { |
15 | | - return { |
16 | | - ...acc, |
17 | | - google: { |
18 | | - clientId: env.GOOGLE_CLIENT_ID, |
19 | | - clientSecret: env.GOOGLE_CLIENT_SECRET, |
20 | | - }, |
21 | | - }; |
22 | | - } |
23 | | - // Add more providers here as needed (github, etc.) |
24 | | - return acc; |
25 | | - }, |
26 | | - {} as Record<string, { clientId: string; clientSecret: string }>, |
27 | | -); |
28 | | - |
29 | | -export const auth = betterAuth({ |
| 7 | +// Create auth instance using the factory function from @refref/auth |
| 8 | +export const auth = getAuth({ |
30 | 9 | baseURL: env.NEXT_PUBLIC_APP_URL, |
31 | | - socialProviders, |
32 | | - database: drizzleAdapter(db, { |
33 | | - provider: "pg", |
34 | | - schema: { |
35 | | - ...schema, |
36 | | - }, |
37 | | - }), |
38 | | - emailAndPassword: { |
39 | | - enabled: env.NEXT_PUBLIC_ENABLE_PASSWORD_AUTH, |
40 | | - }, |
| 10 | + resendApiKey: env.RESEND_API_KEY || "debug_key", |
| 11 | + db, |
| 12 | + schema, |
| 13 | + enabledSocialAuth: env.NEXT_PUBLIC_ENABLED_SOCIAL_AUTH, |
| 14 | + enablePasswordAuth: env.NEXT_PUBLIC_ENABLE_PASSWORD_AUTH, |
| 15 | + enableMagicLinkAuth: env.NEXT_PUBLIC_ENABLE_MAGIC_LINK_AUTH, |
| 16 | + google: env.GOOGLE_CLIENT_ID |
| 17 | + ? { |
| 18 | + clientId: env.GOOGLE_CLIENT_ID, |
| 19 | + clientSecret: env.GOOGLE_CLIENT_SECRET, |
| 20 | + } |
| 21 | + : undefined, |
| 22 | + logger, |
| 23 | + posthog, |
41 | 24 | trustedOrigins: [env.NEXT_PUBLIC_APP_URL], |
42 | | - /* emailVerification: { |
43 | | - sendVerificationEmail: async ({ url }) => { |
44 | | - console.log('verification link', url); |
45 | | - } |
46 | | - }, */ |
47 | | - emailProviders: { |
48 | | - resend: { |
49 | | - enabled: !!env.RESEND_API_KEY && env.RESEND_API_KEY !== "debug_key", |
50 | | - apiKey: env.RESEND_API_KEY || "debug_key", |
51 | | - autosignup: true, |
52 | | - }, |
53 | | - }, |
54 | | - // socialProviders: { |
55 | | - // github: { |
56 | | - // clientId: process.env.GITHUB_CLIENT_ID!, |
57 | | - // clientSecret: process.env.GITHUB_CLIENT_SECRET!, |
58 | | - // } |
59 | | - // }, |
60 | | - plugins: [ |
61 | | - ...(env.NEXT_PUBLIC_ENABLE_MAGIC_LINK_AUTH |
62 | | - ? [ |
63 | | - magicLink({ |
64 | | - sendMagicLink: async ({ email, url }) => { |
65 | | - try { |
66 | | - logger.info("Sending magic link email", { url, email }); |
67 | | - const result = await emailService.sendMagicLink({ email, url }); |
68 | | - |
69 | | - if (!result.success) { |
70 | | - throw ( |
71 | | - result.error || new Error("Failed to send magic link email") |
72 | | - ); |
73 | | - } |
74 | | - |
75 | | - logger.info("Magic link email sent successfully", { email }); |
76 | | - } catch (error) { |
77 | | - console.error("Error sending magic link email", { |
78 | | - error, |
79 | | - email, |
80 | | - }); |
81 | | - logger.error("Error sending magic link email", { |
82 | | - error, |
83 | | - email, |
84 | | - }); |
85 | | - throw error; |
86 | | - } |
87 | | - }, |
88 | | - }), |
89 | | - ] |
90 | | - : []), |
91 | | - organization({ |
92 | | - schema: { |
93 | | - organization: { |
94 | | - modelName: "org", |
95 | | - }, |
96 | | - session: { |
97 | | - modelName: "session", |
98 | | - fields: { |
99 | | - activeOrganizationId: "activeOrganizationId", |
100 | | - }, |
101 | | - }, |
102 | | - member: { |
103 | | - modelName: "orgUser", |
104 | | - fields: { |
105 | | - organizationId: "orgId", |
106 | | - }, |
107 | | - }, |
108 | | - invitation: { |
109 | | - modelName: "invitation", |
110 | | - fields: { |
111 | | - organizationId: "organizationId", |
112 | | - }, |
113 | | - }, |
114 | | - }, |
115 | | - async sendInvitationEmail({ id, email, role, inviter }) { |
116 | | - const inviteLink = `${env.NEXT_PUBLIC_APP_URL}/accept-invitation/${id}`; |
117 | | - logger.info("Sending invitation email", { inviteLink, email, role }); |
118 | | - |
119 | | - await emailService.sendInvitation({ |
120 | | - id, |
121 | | - email, |
122 | | - role, |
123 | | - inviterName: inviter?.user?.name, |
124 | | - inviterEmail: inviter?.user?.email, |
125 | | - inviteLink, |
126 | | - }); |
127 | | - }, |
128 | | - }), |
129 | | - apiKey(), |
130 | | - ], |
131 | | - databaseHooks: { |
132 | | - user: { |
133 | | - create: { |
134 | | - after: async (user) => { |
135 | | - // Track user signup event |
136 | | - posthog.capture({ |
137 | | - distinctId: user.id, |
138 | | - event: "user_sign_up", |
139 | | - properties: { |
140 | | - email: user.email, |
141 | | - name: user.name || undefined, |
142 | | - }, |
143 | | - }); |
144 | | - |
145 | | - // Auto-create default organization for new users |
146 | | - try { |
147 | | - const { org: orgTable, orgUser } = schema; |
148 | | - |
149 | | - // Create default organization |
150 | | - const [newOrg] = await db |
151 | | - .insert(orgTable) |
152 | | - .values({ |
153 | | - name: `${user.name || user.email}'s Organization`, |
154 | | - slug: `org-${user.id.slice(0, 8)}`, |
155 | | - }) |
156 | | - .returning(); |
157 | | - |
158 | | - // Add user as owner of the organization |
159 | | - await db.insert(orgUser).values({ |
160 | | - orgId: newOrg!.id, |
161 | | - userId: user.id, |
162 | | - role: "owner", |
163 | | - }); |
164 | | - |
165 | | - logger.info("Created default organization for new user", { |
166 | | - userId: user.id, |
167 | | - organizationId: newOrg!.id, |
168 | | - }); |
169 | | - } catch (error) { |
170 | | - logger.error("Failed to create default organization for user", { |
171 | | - userId: user.id, |
172 | | - error, |
173 | | - }); |
174 | | - // Don't throw - allow user creation to succeed even if org creation fails |
175 | | - } |
176 | | - }, |
177 | | - }, |
178 | | - }, |
179 | | - }, |
180 | 25 | }); |
0 commit comments