fix build env
This commit is contained in:
+80
-62
@@ -4,52 +4,55 @@ 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
|
||||||
throw new Error(
|
|
||||||
'Missing BETTER_AUTH_SECRET environment variable. Generate one with: openssl rand -base64 32',
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!process.env.BETTER_AUTH_URL) {
|
function getAuth() {
|
||||||
throw new Error(
|
if (!_auth) {
|
||||||
'Missing BETTER_AUTH_URL environment variable. Set it to your app URL (e.g., http://localhost:3000)',
|
const betterAuthSecret = process.env.BETTER_AUTH_SECRET
|
||||||
)
|
const betterAuthUrl = process.env.BETTER_AUTH_URL
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize Resend for email sending
|
if (!betterAuthSecret) {
|
||||||
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null
|
throw new Error(
|
||||||
|
'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 (!betterAuthUrl) {
|
||||||
* Better Auth server instance
|
throw new Error(
|
||||||
* Configured with PostgreSQL database (Supabase) and magic link authentication
|
'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)',
|
||||||
*/
|
)
|
||||||
export const auth = betterAuth({
|
}
|
||||||
database: drizzleAdapter(db, {
|
|
||||||
provider: 'pg',
|
|
||||||
usePlural: true,
|
|
||||||
schema,
|
|
||||||
}),
|
|
||||||
advanced: {
|
|
||||||
database: {
|
|
||||||
generateId: false, // Use our prefixed nanoid IDs from schema
|
|
||||||
},
|
|
||||||
},
|
|
||||||
secret: process.env.BETTER_AUTH_SECRET,
|
|
||||||
baseURL: process.env.BETTER_AUTH_URL,
|
|
||||||
plugins: [
|
|
||||||
magicLink({
|
|
||||||
sendMagicLink: async ({ email, url }) => {
|
|
||||||
if (!resend) {
|
|
||||||
console.log(`[DEV] Magic link for ${email}: ${url}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
// Initialize Resend for email sending
|
||||||
await resend.emails.send({
|
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null
|
||||||
from: 'Pascal <noreply@pascal.app>',
|
|
||||||
to: email,
|
_auth = betterAuth({
|
||||||
subject: 'Sign in to Pascal Editor',
|
database: drizzleAdapter(db, {
|
||||||
html: `
|
provider: 'pg',
|
||||||
|
usePlural: true,
|
||||||
|
schema,
|
||||||
|
}),
|
||||||
|
advanced: {
|
||||||
|
database: {
|
||||||
|
generateId: false, // Use our prefixed nanoid IDs from schema
|
||||||
|
},
|
||||||
|
},
|
||||||
|
secret: betterAuthSecret,
|
||||||
|
baseURL: betterAuthUrl,
|
||||||
|
plugins: [
|
||||||
|
magicLink({
|
||||||
|
sendMagicLink: async ({ email, url }) => {
|
||||||
|
if (!resend) {
|
||||||
|
console.log(`[DEV] Magic link for ${email}: ${url}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await resend.emails.send({
|
||||||
|
from: 'Pascal <noreply@pascal.app>',
|
||||||
|
to: email,
|
||||||
|
subject: 'Sign in to Pascal Editor',
|
||||||
|
html: `
|
||||||
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
||||||
<h2>Sign in to Pascal Editor</h2>
|
<h2>Sign in to Pascal Editor</h2>
|
||||||
<p>Click the button below to sign in to your account:</p>
|
<p>Click the button below to sign in to your account:</p>
|
||||||
@@ -60,27 +63,42 @@ export const auth = betterAuth({
|
|||||||
<p style="color: #666; font-size: 14px;">If you didn't request this email, you can safely ignore it.</p>
|
<p style="color: #666; font-size: 14px;">If you didn't request this email, you can safely ignore it.</p>
|
||||||
</div>
|
</div>
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
console.log(`✓ Magic link email sent to ${email}`)
|
console.log(`✓ Magic link email sent to ${email}`)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to send magic link email:', error)
|
console.error('Failed to send magic link email:', error)
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
session: {
|
||||||
|
expiresIn: 60 * 60 * 24 * 7, // 7 days
|
||||||
|
updateAge: 60 * 60 * 24, // 1 day
|
||||||
|
cookieCache: {
|
||||||
|
enabled: true,
|
||||||
|
maxAge: 5 * 60, // 5 minutes
|
||||||
|
},
|
||||||
|
additionalFields: {
|
||||||
|
activePropertyId: {
|
||||||
|
type: 'string',
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}),
|
})
|
||||||
],
|
}
|
||||||
session: {
|
return _auth
|
||||||
expiresIn: 60 * 60 * 24 * 7, // 7 days
|
}
|
||||||
updateAge: 60 * 60 * 24, // 1 day
|
|
||||||
cookieCache: {
|
/**
|
||||||
enabled: true,
|
* Better Auth server instance
|
||||||
maxAge: 5 * 60, // 5 minutes
|
* Configured with PostgreSQL database (Supabase) and magic link authentication
|
||||||
},
|
*
|
||||||
additionalFields: {
|
* Initialized lazily to avoid requiring env vars at build time
|
||||||
activePropertyId: {
|
*/
|
||||||
type: 'string',
|
export const auth = new Proxy({} as ReturnType<typeof betterAuth>, {
|
||||||
},
|
get(_target, prop) {
|
||||||
},
|
return Reflect.get(getAuth(), prop)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user