import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import { generateApiKey } from '@/lib/auth';
// Get all API keys for the current user
export async function GET(request: NextRequest) {
try {
// Check authentication
const session = await getSession();
if (!session || !session.user) {
return NextResponse.json(
{ error: 'Authentication required' },
{ status: 401 }
);
}
if (!session.user.id) {
return NextResponse.json(
{ error: 'Invalid user session' },
{ status: 401 }
);
}
const userId = parseInt(session.user.id);
// Fetch user's API keys
const keys = await prisma.apiKey.findMany({
where: { userId },
orderBy: { createdAt: 'desc' },
select: {
id: true,
name: true,
createdAt: true,
expiresAt: true,
},
});
return NextResponse.json({ keys });
} catch (error) {
console.error('Error fetching API keys:', error);
return NextResponse.json(
{ error: 'Failed to fetch API keys' },
{ status: 500 }
);
}
}
// Create a new API key
export async function POST(request: NextRequest) {
try {
// Check authentication
const session = await getSession();
if (!session || !session.user) {
return NextResponse.json(
{ error: 'Authentication required' },
{ status: 401 }
);
}
if (!session.user.id) {
return NextResponse.json(
{ error: 'Invalid user session' },
{ status: 401 }
);
}
const userId = parseInt(session.user.id);
const { name } = await request.json();
if (!name) {
return NextResponse.json(
{ error: 'API key name is required' },
{ status: 400 }
);
}
// Generate new API key
const apiKey = await generateApiKey(userId, name);
return NextResponse.json({
success: true,
apiKey
});
} catch (error) {
console.error('Error creating API key:', error);
return NextResponse.json(
{ error: 'Failed to create API key' },
{ status: 500 }
);
}
}