import { createAuth } from '@pascal-app/auth/server' import { db } from '@pascal-app/db' import { Resend } from 'resend' import { env } from '@/env.mjs' import { BASE_URL } from './utils' // Initialize Resend only if API key is available const resend = env.RESEND_API_KEY ? new Resend(env.RESEND_API_KEY) : null export const auth = createAuth({ db, appName: 'Pascal Editor', baseURL: BASE_URL, secret: env.BETTER_AUTH_SECRET, googleClientId: env.GOOGLE_CLIENT_ID, googleClientSecret: env.GOOGLE_CLIENT_SECRET, sendMagicLink: async ({ email, url }) => { if (!resend) { console.log(`[DEV] Magic link for ${email}: ${url}`) return } try { await resend.emails.send({ from: 'Pascal ', to: email, subject: 'Sign in to Pascal Editor', html: `

Sign in to Pascal Editor

Click the button below to sign in to your account:

Sign In

This link will expire in 5 minutes.

If you didn't request this email, you can safely ignore it.

`, }) console.log(`✓ Magic link email sent to ${email}`) } catch (error) { console.error('Failed to send magic link email:', error) throw error } }, }) export type Session = typeof auth.$Infer.Session export type User = typeof auth.$Infer.Session.user