Refactor code structure and remove redundant sections for improved readability and maintainability

This commit is contained in:
Puechberty Arthur
2026-02-23 03:57:05 +01:00
parent 5be8966d66
commit 6760a523e4
27 changed files with 2025 additions and 1087 deletions
+29
View File
@@ -0,0 +1,29 @@
import { NextResponse } from 'next/server';
import {
ADMIN_COOKIE,
createAdminSessionToken,
isValidAdminCredentials
} from '../../../../lib/auth';
export async function POST(request) {
const body = await request.json().catch(() => ({}));
const username = body.username || '';
const password = body.password || '';
if (!isValidAdminCredentials(username, password)) {
return NextResponse.json({ error: 'Identifiants invalides.' }, { status: 401 });
}
const response = NextResponse.json({ ok: true });
response.cookies.set({
name: ADMIN_COOKIE,
value: createAdminSessionToken(),
httpOnly: true,
sameSite: 'strict',
secure: process.env.NODE_ENV === 'production',
path: '/',
maxAge: 60 * 60 * 12
});
return response;
}
+15
View File
@@ -0,0 +1,15 @@
import { NextResponse } from 'next/server';
import { ADMIN_COOKIE } from '../../../../lib/auth';
export async function POST() {
const response = NextResponse.json({ ok: true });
response.cookies.set({
name: ADMIN_COOKIE,
value: '',
path: '/',
httpOnly: true,
sameSite: 'strict',
maxAge: 0
});
return response;
}
+6
View File
@@ -0,0 +1,6 @@
import { NextResponse } from 'next/server';
import { isAdminRequest } from '../../../../lib/auth';
export async function GET(request) {
return NextResponse.json({ authenticated: isAdminRequest(request) });
}
+14
View File
@@ -0,0 +1,14 @@
import { NextResponse } from 'next/server';
import { incrementClick } from '../../../../lib/storage';
export async function POST(request) {
const body = await request.json().catch(() => ({}));
const linkId = body.linkId;
if (!linkId) {
return NextResponse.json({ error: 'linkId requis.' }, { status: 400 });
}
await incrementClick(linkId);
return NextResponse.json({ ok: true });
}
+12
View File
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server';
import { getAnalyticsForLinks } from '../../../lib/storage';
import { isAdminRequest } from '../../../lib/auth';
export async function GET(request) {
if (!isAdminRequest(request)) {
return NextResponse.json({ error: 'Non autorisé.' }, { status: 401 });
}
const analytics = await getAnalyticsForLinks();
return NextResponse.json({ analytics });
}
+17
View File
@@ -0,0 +1,17 @@
import { NextResponse } from 'next/server';
import { removeLink } from '../../../../lib/storage';
import { isAdminRequest } from '../../../../lib/auth';
export async function DELETE(request, { params }) {
if (!isAdminRequest(request)) {
return NextResponse.json({ error: 'Non autorisé.' }, { status: 401 });
}
const { id } = await params;
const deleted = await removeLink(id);
if (!deleted) {
return NextResponse.json({ error: 'Lien introuvable.' }, { status: 404 });
}
return NextResponse.json({ ok: true });
}
+25
View File
@@ -0,0 +1,25 @@
import { NextResponse } from 'next/server';
import { addLink, getLinks } from '../../../lib/storage';
import { isAdminRequest } from '../../../lib/auth';
export async function GET() {
const links = await getLinks();
return NextResponse.json({ links });
}
export async function POST(request) {
if (!isAdminRequest(request)) {
return NextResponse.json({ error: 'Non autorisé.' }, { status: 401 });
}
const payload = await request.json().catch(() => ({}));
const required = ['name', 'url'];
const missing = required.some((field) => !payload[field]);
if (missing) {
return NextResponse.json({ error: 'Nom et URL sont obligatoires.' }, { status: 400 });
}
const created = await addLink(payload);
return NextResponse.json({ link: created }, { status: 201 });
}