Ajout du système d'administration avec token unique et QR code
- Implémentation complète du système d'administration (/admin) - Gestion des clients avec base de données JSON - Génération de token unique et QR code pour chaque client - Intégration des données client dans l'application (bungalow, WiFi, message) - Amélioration du composant WifiCard avec fallback de copie - Optimisation du hook useClientData pour chargement immédiat - Ajout de la variable d'environnement ADMIN_PASSWORD
This commit is contained in:
55
app/api/admin/clients/[id]/route.ts
Normal file
55
app/api/admin/clients/[id]/route.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { updateClient, deleteClient, loadClients } from "@/lib/admin/client-utils";
|
||||
import { requireAdminAuth } from "@/lib/admin/auth";
|
||||
import { ClientInput } from "@/lib/types/client";
|
||||
|
||||
export async function PUT(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
if (!requireAdminAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
const body: Partial<ClientInput> = await request.json();
|
||||
const client = updateClient(id, body);
|
||||
|
||||
if (!client) {
|
||||
return NextResponse.json(
|
||||
{ error: "Client non trouvé" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(client);
|
||||
} catch (error: any) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message || "Erreur lors de la mise à jour" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
if (!requireAdminAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { id } = await params;
|
||||
const deleted = deleteClient(id);
|
||||
|
||||
if (!deleted) {
|
||||
return NextResponse.json(
|
||||
{ error: "Client non trouvé" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
}
|
||||
|
||||
60
app/api/admin/clients/route.ts
Normal file
60
app/api/admin/clients/route.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import {
|
||||
createClient,
|
||||
loadClients,
|
||||
updateClient,
|
||||
deleteClient,
|
||||
validateEmail,
|
||||
} from "@/lib/admin/client-utils";
|
||||
import { requireAdminAuth } from "@/lib/admin/auth";
|
||||
import { ClientInput } from "@/lib/types/client";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (!requireAdminAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
const clients = loadClients();
|
||||
return NextResponse.json(clients);
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
if (!requireAdminAuth(request)) {
|
||||
return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const body: ClientInput = await request.json();
|
||||
|
||||
// Validation
|
||||
if (!body.email || !validateEmail(body.email)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Email invalide" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!body.bungalowNumber) {
|
||||
return NextResponse.json(
|
||||
{ error: "Numéro de bungalow requis" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const client = createClient({
|
||||
email: body.email,
|
||||
bungalowNumber: body.bungalowNumber,
|
||||
wifiName: body.wifiName || "Lagon-WiFi",
|
||||
wifiPassword: body.wifiPassword || "",
|
||||
gerantMessage: body.gerantMessage || "Bienvenue dans notre pension de famille !",
|
||||
});
|
||||
|
||||
return NextResponse.json(client, { status: 201 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json(
|
||||
{ error: error.message || "Erreur lors de la création du client" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
26
app/api/client/[token]/route.ts
Normal file
26
app/api/client/[token]/route.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { getClientByToken } from "@/lib/admin/client-utils";
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ token: string }> }
|
||||
) {
|
||||
const { token } = await params;
|
||||
const client = getClientByToken(token);
|
||||
|
||||
if (!client) {
|
||||
return NextResponse.json(
|
||||
{ error: "Token invalide ou expiré" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
// Retourner uniquement les informations nécessaires (sans le token)
|
||||
return NextResponse.json({
|
||||
bungalowNumber: client.bungalowNumber,
|
||||
wifiName: client.wifiName,
|
||||
wifiPassword: client.wifiPassword,
|
||||
gerantMessage: client.gerantMessage,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user