fix build env

This commit is contained in:
wass08
2026-02-11 16:35:56 +09:00
parent d6b8fb2cf2
commit d37a084715
+29 -11
View File
@@ -4,26 +4,29 @@ import { drizzleAdapter } from 'better-auth/adapters/drizzle'
import { magicLink } from 'better-auth/plugins'
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(
'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(
'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
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null
/**
* Better Auth server instance
* Configured with PostgreSQL database (Supabase) and magic link authentication
*/
export const auth = betterAuth({
_auth = betterAuth({
database: drizzleAdapter(db, {
provider: 'pg',
usePlural: true,
@@ -34,8 +37,8 @@ export const auth = betterAuth({
generateId: false, // Use our prefixed nanoid IDs from schema
},
},
secret: process.env.BETTER_AUTH_SECRET,
baseURL: process.env.BETTER_AUTH_URL,
secret: betterAuthSecret,
baseURL: betterAuthUrl,
plugins: [
magicLink({
sendMagicLink: async ({ email, url }) => {
@@ -83,6 +86,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)
},
})
/**
* Type helpers for better-auth session