fix magic link issue

This commit is contained in:
wass08
2026-02-11 17:09:08 +09:00
parent 64205b1077
commit 92f1c065f8
6 changed files with 129 additions and 221 deletions
+3 -21
View File
@@ -3,27 +3,9 @@
* Handles all /api/auth/* routes for authentication
*/
import { auth } from '@pascal-app/auth/server'
import { toNextJsHandler } from 'better-auth/next-js'
import type { NextRequest } from 'next/server'
import { auth } from '@/lib/auth'
// Lazy initialization of the handler
let handler: ReturnType<typeof toNextJsHandler> | null = null
const { GET, POST } = toNextJsHandler(auth)
function getHandler() {
if (!handler) {
handler = toNextJsHandler(auth)
}
return handler
}
// Export route handlers that initialize lazily
export async function GET(request: NextRequest) {
const handlers = getHandler()
return handlers.GET(request)
}
export async function POST(request: NextRequest) {
const handlers = getHandler()
return handlers.POST(request)
}
export { GET, POST }
+44
View File
@@ -0,0 +1,44 @@
import { createAuth } from '@pascal-app/auth/server'
import { db } from '@pascal-app/db'
import { Resend } from 'resend'
const resend = process.env.RESEND_API_KEY ? new Resend(process.env.RESEND_API_KEY) : null
export const auth = createAuth({
db,
appName: 'Pascal Editor',
baseURL: process.env.BETTER_AUTH_URL!,
secret: process.env.BETTER_AUTH_SECRET!,
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;">
<h2>Sign in to Pascal Editor</h2>
<p>Click the button below to sign in to your account:</p>
<a href="${url}" style="display: inline-block; background-color: #000; color: #fff; padding: 12px 24px; text-decoration: none; border-radius: 6px; margin: 20px 0;">
Sign In
</a>
<p style="color: #666; font-size: 14px;">This link will expire in 5 minutes.</p>
<p style="color: #666; font-size: 14px;">If you didn't request this email, you can safely ignore it.</p>
</div>
`,
})
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