import { NextRequest, NextResponse } from 'next/server' export async function POST(request: NextRequest) { try { const body = await request.json() const { name, email, reason } = body // Validate required fields if (!name || !email) { return NextResponse.json( { error: 'Name and email are required' }, { status: 400 } ) } // Email validation const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ if (!emailRegex.test(email)) { return NextResponse.json( { error: 'Invalid email format' }, { status: 400 } ) } // Send notification email via Resend const resendApiKey = process.env.RESEND_API_KEY if (!resendApiKey) { console.error('RESEND_API_KEY not configured') return NextResponse.json( { error: 'Email service not configured' }, { status: 500 } ) } const adminEmail = process.env.ADMIN_EMAIL || 'jeff@jeffemmett.com' const emailResponse = await fetch('https://api.resend.com/emails', { method: 'POST', headers: { 'Authorization': `Bearer ${resendApiKey}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ from: 'Jefflix ', to: adminEmail, subject: `[Jefflix] New Access Request from ${name}`, html: `

New Jefflix Access Request

Someone has requested access to Jefflix:

Name: ${escapeHtml(name)}
Email: ${escapeHtml(email)}
Reason: ${escapeHtml(reason || 'Not provided')}
Requested: ${new Date().toLocaleString()}

To approve this request:

  1. Go to Jellyfin Dashboard
  2. Navigate to Dashboard → Users → Add User
  3. Create an account for ${escapeHtml(name)} (${escapeHtml(email)})
  4. Reply to this email to let them know their account is ready

This is an automated message from Jefflix.

`, }), }) if (!emailResponse.ok) { const errorData = await emailResponse.json() console.error('Resend API error:', errorData) return NextResponse.json( { error: 'Failed to send notification' }, { status: 500 } ) } return NextResponse.json({ success: true }) } catch (error) { console.error('Request access error:', error) return NextResponse.json( { error: 'Internal server error' }, { status: 500 } ) } } function escapeHtml(text: string): string { const map: Record = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', } return text.replace(/[&<>"']/g, (char) => map[char]) }