Files
portfolio/app/api/projects/export/route.ts
denshooter 9072faae43 refactor: enhance security and performance in configuration and API routes
- Update Content Security Policy (CSP) in next.config.ts to avoid `unsafe-eval` in production, improving security against XSS attacks.
- Refactor API routes to enforce admin authentication and session validation, ensuring secure access to sensitive endpoints.
- Optimize analytics data retrieval by using database aggregation instead of loading all records into memory, improving performance and reducing memory usage.
- Implement session token creation and verification for better session management and security across the application.
- Enhance error handling and input validation in various API routes to ensure robustness and prevent potential issues.
2026-01-11 22:44:26 +01:00

65 lines
2.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { projectService } from '@/lib/prisma';
import { requireSessionAuth } from '@/lib/auth';
export async function GET(request: NextRequest) {
try {
const isAdminRequest = request.headers.get('x-admin-request') === 'true';
if (!isAdminRequest) return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
const authError = requireSessionAuth(request);
if (authError) return authError;
// Get all projects with full data
const projectsResult = await projectService.getAllProjects();
const projects = projectsResult.projects || projectsResult;
// Format for export
const exportData = {
version: '1.0',
exportDate: new Date().toISOString(),
projects: projects.map(project => ({
id: project.id,
title: project.title,
description: project.description,
content: project.content,
tags: project.tags,
category: project.category,
featured: project.featured,
github: project.github,
live: project.live,
published: project.published,
imageUrl: project.imageUrl,
difficulty: project.difficulty,
timeToComplete: project.timeToComplete,
technologies: project.technologies,
challenges: project.challenges,
lessonsLearned: project.lessonsLearned,
futureImprovements: project.futureImprovements,
demoVideo: project.demoVideo,
screenshots: project.screenshots,
colorScheme: project.colorScheme,
accessibility: project.accessibility,
performance: project.performance,
analytics: project.analytics,
createdAt: project.createdAt,
updatedAt: project.updatedAt
}))
};
// Return as downloadable JSON file
return new NextResponse(JSON.stringify(exportData, null, 2), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Content-Disposition': `attachment; filename="portfolio-projects-${new Date().toISOString().split('T')[0]}.json"`
}
});
} catch (error) {
console.error('Export error:', error);
return NextResponse.json(
{ error: 'Failed to export projects' },
{ status: 500 }
);
}
}