"use client"; import { useState } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { useToast } from "@/components/ui/use-toast"; import { Lock } from "lucide-react"; export function ChangePasswordForm() { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const { toast } = useToast(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (newPassword !== confirmPassword) { toast({ variant: "destructive", title: "Erreur", description: "Les mots de passe ne correspondent pas", }); return; } if (newPassword.length < 8) { toast({ variant: "destructive", title: "Erreur", description: "Le mot de passe doit contenir au moins 8 caractères", }); return; } setIsLoading(true); try { const response = await fetch("/api/user/password", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ currentPassword, newPassword }), }); if (!response.ok) { const data = await response.json(); throw new Error(data.error || "Erreur lors du changement de mot de passe"); } toast({ title: "Succès", description: "Votre mot de passe a été modifié avec succès", }); // Reset form setCurrentPassword(""); setNewPassword(""); setConfirmPassword(""); } catch (error) { toast({ variant: "destructive", title: "Erreur", description: error instanceof Error ? error.message : "Une erreur est survenue", }); } finally { setIsLoading(false); } }; return ( Changer le mot de passe Assurez-vous d'utiliser un mot de passe fort (8 caractères minimum, une majuscule et un chiffre)
setCurrentPassword(e.target.value)} className="pl-9" required disabled={isLoading} />
setNewPassword(e.target.value)} className="pl-9" required disabled={isLoading} />
setConfirmPassword(e.target.value)} className="pl-9" required disabled={isLoading} />
); }