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:
85
app/admin/login/page.tsx
Normal file
85
app/admin/login/page.tsx
Normal file
@ -0,0 +1,85 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card";
|
||||
import Logo from "@/components/Logo";
|
||||
|
||||
export default function AdminLoginPage() {
|
||||
const [password, setPassword] = useState("");
|
||||
const [error, setError] = useState("");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError("");
|
||||
setLoading(true);
|
||||
|
||||
// Vérification simple côté client (la vraie vérification se fait côté serveur)
|
||||
// Pour l'instant, on stocke le mot de passe dans localStorage
|
||||
localStorage.setItem("adminPassword", password);
|
||||
|
||||
// Test avec une requête API
|
||||
try {
|
||||
const response = await fetch("/api/admin/clients", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${password}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
router.push("/admin");
|
||||
} else {
|
||||
setError("Mot de passe incorrect");
|
||||
localStorage.removeItem("adminPassword");
|
||||
}
|
||||
} catch (err) {
|
||||
setError("Erreur de connexion");
|
||||
localStorage.removeItem("adminPassword");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-background flex items-center justify-center px-4">
|
||||
<Card className="w-full max-w-md">
|
||||
<CardHeader className="text-center">
|
||||
<div className="flex justify-center mb-4">
|
||||
<Logo size={100} />
|
||||
</div>
|
||||
<CardTitle>Administration</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label className="block text-sm font-medium text-gray-700 mb-1">
|
||||
Mot de passe
|
||||
</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
className="w-full px-4 py-2 border border-gray-300 rounded-xl focus:ring-2 focus:ring-primary focus:border-transparent"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-xl text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Button type="submit" disabled={loading} className="w-full">
|
||||
{loading ? "Connexion..." : "Se connecter"}
|
||||
</Button>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
73
app/admin/page.tsx
Normal file
73
app/admin/page.tsx
Normal file
@ -0,0 +1,73 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import AdminLayout from "@/components/admin/AdminLayout";
|
||||
import ClientForm from "@/components/admin/ClientForm";
|
||||
import ClientList from "@/components/admin/ClientList";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Plus } from "lucide-react";
|
||||
import { Client } from "@/lib/types/client";
|
||||
|
||||
export default function AdminPage() {
|
||||
const [showForm, setShowForm] = useState(false);
|
||||
const [editingClient, setEditingClient] = useState<Client | undefined>();
|
||||
const [refreshKey, setRefreshKey] = useState(0);
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
// Vérifier si l'admin est connecté
|
||||
const adminPassword = localStorage.getItem("adminPassword");
|
||||
if (!adminPassword) {
|
||||
router.push("/admin/login");
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
const handleNewClient = () => {
|
||||
setEditingClient(undefined);
|
||||
setShowForm(true);
|
||||
};
|
||||
|
||||
const handleEdit = (client: Client) => {
|
||||
setEditingClient(client);
|
||||
setShowForm(true);
|
||||
};
|
||||
|
||||
const handleSuccess = () => {
|
||||
setShowForm(false);
|
||||
setEditingClient(undefined);
|
||||
setRefreshKey((k) => k + 1);
|
||||
};
|
||||
|
||||
const handleCancel = () => {
|
||||
setShowForm(false);
|
||||
setEditingClient(undefined);
|
||||
};
|
||||
|
||||
return (
|
||||
<AdminLayout>
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h2 className="text-2xl font-bold text-primary">Gestion des clients</h2>
|
||||
{!showForm && (
|
||||
<Button onClick={handleNewClient}>
|
||||
<Plus className="h-4 w-4 mr-2" />
|
||||
Nouveau client
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{showForm ? (
|
||||
<ClientForm
|
||||
client={editingClient}
|
||||
onSuccess={handleSuccess}
|
||||
onCancel={handleCancel}
|
||||
/>
|
||||
) : (
|
||||
<ClientList onEdit={handleEdit} onRefresh={() => setRefreshKey((k) => k + 1)} />
|
||||
)}
|
||||
</div>
|
||||
</AdminLayout>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user