Files
spsays-admin/app/forgot-password/page.tsx
T
2026-06-16 14:50:09 +05:30

69 lines
2.2 KiB
TypeScript

"use client";
import { useState } from "react";
import Link from "next/link";
import { api, AdminError } from "@/lib/api";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { AuthShell, Field } from "@/app/login/page";
export default function ForgotPasswordPage() {
const [email, setEmail] = useState("");
const [busy, setBusy] = useState(false);
const [sent, setSent] = useState(false);
const [error, setError] = useState<string | null>(null);
async function onSubmit(e: React.FormEvent) {
e.preventDefault();
setError(null);
setBusy(true);
try {
await api.forgotPassword(email.trim());
// The BFF always responds 200 (it doesn't reveal whether an account
// exists), so we show the same confirmation regardless.
setSent(true);
} catch (err) {
setError(err instanceof AdminError ? err.message : "Something went wrong.");
} finally {
setBusy(false);
}
}
return (
<AuthShell title="Reset your password">
{sent ? (
<div className="space-y-4 text-sm text-muted-foreground">
<p>
If an account exists for <strong>{email}</strong>, we&apos;ve sent a
password-reset link. Check your inbox.
</p>
<Link href="/login" className="text-primary hover:underline">
Back to sign in
</Link>
</div>
) : (
<form onSubmit={onSubmit} className="space-y-4">
<Field label="Email">
<Input
type="email"
autoComplete="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
</Field>
{error ? <p className="text-sm text-destructive">{error}</p> : null}
<Button type="submit" disabled={busy} className="w-full">
{busy ? "Sending…" : "Send reset link"}
</Button>
<p className="text-center text-sm text-muted-foreground">
<Link href="/login" className="hover:text-foreground">
Back to sign in
</Link>
</p>
</form>
)}
</AuthShell>
);
}