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

105 lines
3.3 KiB
TypeScript

"use client";
import { cn } from "@/lib/utils";
import { resetPassword } 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 Link from "next/link";
import { useState } from "react";
export function ForgotPasswordForm({
className,
...props
}: React.ComponentPropsWithoutRef<"div">) {
const [email, setEmail] = useState("");
const [error, setError] = useState<string | null>(null);
const [success, setSuccess] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const handleForgotPassword = async (e: React.FormEvent) => {
e.preventDefault();
setIsLoading(true);
setError(null);
try {
const res = await resetPassword(email);
if (res.success) {
setSuccess(true);
} 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}>
{success ? (
<Card>
<CardHeader>
<CardTitle className="text-2xl">E-Mails prüfen</CardTitle>
<CardDescription>Link zum Zurücksetzen gesendet</CardDescription>
</CardHeader>
<CardContent>
<p className="text-sm text-muted-foreground">
Wenn Sie sich mit E-Mail und Passwort registriert haben, erhalten Sie
in Kürze eine E-Mail zum Zurücksetzen Ihres Passworts.
</p>
</CardContent>
</Card>
) : (
<Card>
<CardHeader>
<CardTitle className="text-2xl">Passwort zurücksetzen</CardTitle>
<CardDescription>
Geben Sie Ihre E-Mail-Adresse ein. Wir senden Ihnen einen Link,
um Ihr Passwort zurückzusetzen.
</CardDescription>
</CardHeader>
<CardContent>
<form onSubmit={handleForgotPassword}>
<div className="flex flex-col gap-6">
<div className="grid gap-2">
<Label htmlFor="email">E-Mail</Label>
<Input
id="email"
type="email"
placeholder="name@beispiel.de"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</div>
{error && <p className="text-sm text-red-500">{error}</p>}
<Button type="submit" className="w-full" disabled={isLoading}>
{isLoading ? "Wird gesendet..." : "Link senden"}
</Button>
</div>
<div className="mt-4 text-center text-sm">
Bereits ein Konto vorhanden?{" "}
<Link
href="/auth/login"
className="underline underline-offset-4"
>
Anmelden
</Link>
</div>
</form>
</CardContent>
</Card>
)}
</div>
);
}