import { NextRequest, NextResponse } from 'next/server' import nodemailer from 'nodemailer' 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 } ) } const smtpHost = process.env.SMTP_HOST const smtpUser = process.env.SMTP_USER const smtpPass = process.env.SMTP_PASS if (!smtpHost || !smtpUser || !smtpPass) { console.error('SMTP credentials not configured') return NextResponse.json( { error: 'Email service not configured' }, { status: 500 } ) } const adminEmail = process.env.ADMIN_EMAIL || 'jeff@jeffemmett.com' const transporter = nodemailer.createTransport({ host: smtpHost, port: Number(process.env.SMTP_PORT) || 587, secure: false, auth: { user: smtpUser, pass: smtpPass }, tls: { rejectUnauthorized: false }, }) await transporter.sendMail({ from: `Jefflix <${smtpUser}>`, 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.

`, }) 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]) }