Files
stripstream/src/components/account/ChangePasswordForm.tsx

140 lines
4.3 KiB
TypeScript

"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 (
<Card>
<CardHeader>
<CardTitle>Changer le mot de passe</CardTitle>
<CardDescription>
Assurez-vous d&apos;utiliser un mot de passe fort (8 caractères minimum, une majuscule et
un chiffre)
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleSubmit} className="space-y-4">
<div className="space-y-2">
<Label htmlFor="currentPassword">Mot de passe actuel</Label>
<div className="relative">
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
<Input
id="currentPassword"
type="password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
className="pl-9"
required
disabled={isLoading}
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="newPassword">Nouveau mot de passe</Label>
<div className="relative">
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
<Input
id="newPassword"
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
className="pl-9"
required
disabled={isLoading}
/>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="confirmPassword">Confirmer le mot de passe</Label>
<div className="relative">
<Lock className="absolute left-3 top-3 h-4 w-4 text-muted-foreground" />
<Input
id="confirmPassword"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
className="pl-9"
required
disabled={isLoading}
/>
</div>
</div>
<Button type="submit" disabled={isLoading}>
{isLoading ? "Modification..." : "Changer le mot de passe"}
</Button>
</form>
</CardContent>
</Card>
);
}