jefflix-website/app/api/request-access/route.ts

110 lines
3.7 KiB
TypeScript

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 <noreply@jefflix.lol>',
to: adminEmail,
subject: `[Jefflix] New Access Request from ${name}`,
html: `
<h2>New Jefflix Access Request</h2>
<p>Someone has requested access to Jefflix:</p>
<table style="border-collapse: collapse; margin: 20px 0;">
<tr>
<td style="padding: 8px; font-weight: bold; border: 1px solid #ddd;">Name:</td>
<td style="padding: 8px; border: 1px solid #ddd;">${escapeHtml(name)}</td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold; border: 1px solid #ddd;">Email:</td>
<td style="padding: 8px; border: 1px solid #ddd;"><a href="mailto:${escapeHtml(email)}">${escapeHtml(email)}</a></td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold; border: 1px solid #ddd;">Reason:</td>
<td style="padding: 8px; border: 1px solid #ddd;">${escapeHtml(reason || 'Not provided')}</td>
</tr>
<tr>
<td style="padding: 8px; font-weight: bold; border: 1px solid #ddd;">Requested:</td>
<td style="padding: 8px; border: 1px solid #ddd;">${new Date().toLocaleString()}</td>
</tr>
</table>
<p>To approve this request:</p>
<ol>
<li>Go to <a href="https://movies.jefflix.lol">Jellyfin Dashboard</a></li>
<li>Navigate to Dashboard → Users → Add User</li>
<li>Create an account for ${escapeHtml(name)} (${escapeHtml(email)})</li>
<li>Reply to this email to let them know their account is ready</li>
</ol>
<hr style="margin: 20px 0; border: none; border-top: 1px solid #ddd;" />
<p style="color: #666; font-size: 12px;">This is an automated message from Jefflix.</p>
`,
}),
})
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<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;',
}
return text.replace(/[&<>"']/g, (char) => map[char])
}