Files
webshop/shop/components/update-password-form.tsx
2026-07-03 13:34:43 +02:00

106 lines
3.4 KiB
TypeScript

"use client";
import { cn } from "@/lib/utils";
import { updatePassword } from "@/lib/actions/auth";
import { Button } from "@/components/ui/button";
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useRouter } from "next/navigation";
import { useState } from "react";
export function UpdatePasswordForm({
className,
...props
}: React.ComponentPropsWithoutRef<"div">) {
const [password, setPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [error, setError] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
const router = useRouter();
const handleForgotPassword = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError(null);
if (password.length < 6) {
setError("Passwort muss mindestens 6 Zeichen lang sein.");
setIsLoading(false);
return;
}
if (password !== confirmPassword) {
setError("Die Passwörter stimmen nicht überein.");
setIsLoading(false);
return;
}
try {
const res = await updatePassword(password);
if (res.success) {
router.push("/");
} else {
setError(res.error || "Ein Fehler ist aufgetreten");
}
} catch (error: unknown) {
setError(error instanceof Error ? error.message : "Ein Fehler ist aufgetreten");
} finally {
setIsLoading(false);
}
};
return (
<div className={cn("flex flex-col gap-6", className)} {...props}>
<Card className="glass-dark border-white/10 text-white shadow-2xl">
<CardHeader>
<CardTitle className="text-2xl">Neues Passwort festlegen</CardTitle>
<CardDescription className="text-slate-300">
Bitte geben Sie Ihr neues Passwort ein und bestätigen Sie es.
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleForgotPassword}>
<div className="flex flex-col gap-6">
<div className="grid gap-2">
<Label htmlFor="password">Neues Passwort</Label>
<Input
id="password"
type="password"
placeholder="Neues Passwort"
required
value={password}
onChange={(e) => setPassword(e.target.value)}
className="bg-white/5 border-white/10 text-white"
/>
</div>
<div className="grid gap-2">
<Label htmlFor="confirmPassword">Passwort bestätigen</Label>
<Input
id="confirmPassword"
type="password"
placeholder="Passwort bestätigen"
required
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
className="bg-white/5 border-white/10 text-white"
/>
</div>
{error && <p className="text-sm text-red-500">{error}</p>}
<Button type="submit" className="w-full bg-primary hover:bg-primary/90" disabled={isLoading}>
{isLoading ? "Speichern..." : "Neues Passwort speichern"}
</Button>
</div>
</form>
</CardContent>
</Card>
</div>
);
}