fix build env

This commit is contained in:
wass08
2026-02-11 16:35:56 +09:00
parent d6b8fb2cf2
commit d37a084715
+33 -15
View File
@@ -4,26 +4,29 @@ import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { magicLink } from 'better-auth/plugins' import { magicLink } from 'better-auth/plugins'
import { Resend } from 'resend' import { Resend } from 'resend'
if (!process.env.BETTER_AUTH_SECRET) { let _auth: ReturnType<typeof betterAuth> | null = null
function getAuth() {
if (!_auth) {
const betterAuthSecret = process.env.BETTER_AUTH_SECRET
const betterAuthUrl = process.env.BETTER_AUTH_URL
if (!betterAuthSecret) {
throw new Error( throw new Error(
'Missing BETTER_AUTH_SECRET environment variable. Generate one with: openssl rand -base64 32', 'Missing BETTER_AUTH_SECRET environment variable. Please configure it in your deployment settings or .env.local file. Generate one with: openssl rand -base64 32',
) )
} }
if (!process.env.BETTER_AUTH_URL) { if (!betterAuthUrl) {
throw new Error( throw new Error(
'Missing BETTER_AUTH_URL environment variable. Set it to your app URL (e.g., http://localhost:3000)', 'Missing BETTER_AUTH_URL environment variable. Please configure it in your deployment settings or .env.local file. Set it to your app URL (e.g., http://localhost:3000 or https://yourdomain.com)',
) )
} }
// Initialize Resend for email sending // Initialize Resend for email sending
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null
/** _auth = betterAuth({
* Better Auth server instance
* Configured with PostgreSQL database (Supabase) and magic link authentication
*/
export const auth = betterAuth({
database: drizzleAdapter(db, { database: drizzleAdapter(db, {
provider: 'pg', provider: 'pg',
usePlural: true, usePlural: true,
@@ -34,8 +37,8 @@ export const auth = betterAuth({
generateId: false, // Use our prefixed nanoid IDs from schema generateId: false, // Use our prefixed nanoid IDs from schema
}, },
}, },
secret: process.env.BETTER_AUTH_SECRET, secret: betterAuthSecret,
baseURL: process.env.BETTER_AUTH_URL, baseURL: betterAuthUrl,
plugins: [ plugins: [
magicLink({ magicLink({
sendMagicLink: async ({ email, url }) => { sendMagicLink: async ({ email, url }) => {
@@ -82,6 +85,21 @@ export const auth = betterAuth({
}, },
}, },
}, },
})
}
return _auth
}
/**
* Better Auth server instance
* Configured with PostgreSQL database (Supabase) and magic link authentication
*
* Initialized lazily to avoid requiring env vars at build time
*/
export const auth = new Proxy({} as ReturnType<typeof betterAuth>, {
get(_target, prop) {
return Reflect.get(getAuth(), prop)
},
}) })
/** /**