11import 'server-only' ;
22import { betterAuth } from 'better-auth' ;
3- // Use the adapter import path potentially provided by the core library or a plugin
43import { drizzleAdapter } from "better-auth/adapters/drizzle" ;
54import { nextCookies } from 'better-auth/next-js' ;
6- import { db , user as schemaUser } from '@snow-leopard/db' ; // Use the actual package name
7- import * as schema from '@snow-leopard/db' ; // Import all exports if needed, or specific tables
8- import Stripe from "stripe" ; // Import Stripe SDK
9- import { stripe } from "@better-auth/stripe" ; // Import Better Auth Stripe plugin
10- import { Resend } from 'resend' ; // Import Resend SDK
11- // import { count } from 'drizzle-orm'; // No longer needed for test query
12-
13- // Check for required environment variables
5+ import { db , user as schemaUser } from '@snow-leopard/db' ;
6+ import * as schema from '@snow-leopard/db' ;
7+ import Stripe from "stripe" ;
8+ import { stripe } from "@better-auth/stripe" ;
9+ import { Resend } from 'resend' ;
10+
11+ const googleEnabled = process . env . GOOGLE_ENABLED === 'true' ;
12+ const githubEnabled = process . env . GITHUB_ENABLED === 'true' ;
13+
1414if ( ! process . env . BETTER_AUTH_SECRET ) {
1515 throw new Error ( 'Missing BETTER_AUTH_SECRET environment variable' ) ;
1616}
1717if ( ! process . env . BETTER_AUTH_URL ) {
1818 throw new Error ( 'Missing BETTER_AUTH_URL environment variable' ) ;
1919}
2020
21- // Only check Stripe keys if Stripe is explicitly enabled
2221if ( process . env . STRIPE_ENABLED === 'true' ) {
2322 if ( ! process . env . STRIPE_SECRET_KEY ) throw new Error ( 'Missing STRIPE_SECRET_KEY but STRIPE_ENABLED is true' ) ;
2423 if ( ! process . env . STRIPE_WEBHOOK_SECRET ) throw new Error ( 'Missing STRIPE_WEBHOOK_SECRET but STRIPE_ENABLED is true' ) ;
2524 if ( ! process . env . STRIPE_PRO_MONTHLY_PRICE_ID ) throw new Error ( 'Missing STRIPE_PRO_MONTHLY_PRICE_ID but STRIPE_ENABLED is true' ) ; // Example Price ID env var
2625 if ( ! process . env . STRIPE_PRO_YEARLY_PRICE_ID ) throw new Error ( 'Missing STRIPE_PRO_YEARLY_PRICE_ID but STRIPE_ENABLED is true' ) ; // Add check for yearly price ID
2726}
2827
29- // ---> Add check for email verification enabled <---
3028const emailVerificationEnabled = process . env . EMAIL_VERIFY_ENABLED === 'true' ;
3129console . log ( `Email Verification Enabled: ${ emailVerificationEnabled } ` ) ;
3230
33- // Check Resend/Email From keys ONLY if verification is enabled
3431if ( emailVerificationEnabled ) {
3532 if ( ! process . env . RESEND_API_KEY ) throw new Error ( 'Missing RESEND_API_KEY because EMAIL_VERIFY_ENABLED is true' ) ;
3633 if ( ! process . env . EMAIL_FROM ) throw new Error ( 'Missing EMAIL_FROM because EMAIL_VERIFY_ENABLED is true' ) ;
3734}
3835
39- // Environment Variable Checks (Ensure all required vars are present)
4036if ( process . env . STRIPE_ENABLED === 'true' ) {
4137 if ( ! process . env . STRIPE_SECRET_KEY ) {
4238 throw new Error ( "STRIPE_SECRET_KEY environment variable is missing but STRIPE_ENABLED is true." ) ;
@@ -52,6 +48,17 @@ if (process.env.STRIPE_ENABLED === 'true') {
5248 }
5349}
5450
51+ if ( googleEnabled ) {
52+ if ( ! process . env . GOOGLE_CLIENT_ID ) throw new Error ( 'Missing GOOGLE_CLIENT_ID because GOOGLE_ENABLED is true' ) ;
53+ if ( ! process . env . GOOGLE_CLIENT_SECRET ) throw new Error ( 'Missing GOOGLE_CLIENT_SECRET because GOOGLE_ENABLED is true' ) ;
54+ console . log ( 'Google OAuth ENABLED' ) ;
55+ }
56+
57+ if ( githubEnabled ) {
58+ if ( ! process . env . GITHUB_CLIENT_ID ) throw new Error ( 'Missing GITHUB_CLIENT_ID because GITHUB_ENABLED is true' ) ;
59+ if ( ! process . env . GITHUB_CLIENT_SECRET ) throw new Error ( 'Missing GITHUB_CLIENT_SECRET because GITHUB_ENABLED is true' ) ;
60+ console . log ( 'GitHub OAuth ENABLED' ) ;
61+ }
5562
5663console . log ( '--- Checking env vars in lib/auth.ts ---' ) ;
5764console . log ( 'DATABASE_URL:' , process . env . DATABASE_URL ? 'Set' : 'MISSING!' ) ;
@@ -115,14 +122,31 @@ if (process.env.STRIPE_ENABLED === 'true') {
115122 console . log ( 'Stripe is DISABLED. Skipping Stripe plugin for Better Auth.' ) ;
116123}
117124
118- // Ensure the nextCookies plugin is always last in the chain
119125authPlugins . push ( nextCookies ( ) ) ;
120126
127+ const socialProviders : Record < string , any > = { } ;
128+
129+ if ( googleEnabled ) {
130+ socialProviders . google = {
131+ clientId : process . env . GOOGLE_CLIENT_ID ! ,
132+ clientSecret : process . env . GOOGLE_CLIENT_SECRET ! ,
133+ } ;
134+ }
135+
136+ if ( githubEnabled ) {
137+ socialProviders . github = {
138+ clientId : process . env . GITHUB_CLIENT_ID ! ,
139+ clientSecret : process . env . GITHUB_CLIENT_SECRET ! ,
140+ } ;
141+ }
142+
121143export const auth = betterAuth ( {
122144 database : drizzleAdapter ( db , {
123145 provider : 'pg' ,
124- } ) ,
125-
146+ } ) ,
147+
148+ socialProviders,
149+
126150 emailAndPassword : {
127151 enabled : true ,
128152 requireEmailVerification : emailVerificationEnabled ,
0 commit comments