mirror of
https://github.com/arthur-pbty/links.git
synced 2026-08-01 20:29:27 +02:00
Refactor code structure and remove redundant sections for improved readability and maintainability
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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) });
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
Reference in New Issue
Block a user