Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 155x 155x 155x 155x 155x 7x 7x 7x 7x 7x 3x 3x 2x 2x 1x 1x 1x 5x 155x 1x 155x 4x 151x 131x | "use client";
/**
* ForgotPassword - Request password reset flow
* @see tests/components/auth/ForgotPassword.test.tsx
* @see JCN-28
*/
import { useState } from "react";
import { resetPassword } from "aws-amplify/auth";
import {
Card,
CardContent,
CardDescription,
CardFooter,
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 { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Loader2, AlertCircle, Mail, CheckCircle2 } from "lucide-react";
interface ForgotPasswordProps {
onCodeSent?: (email: string) => void;
}
export function ForgotPassword({ onCodeSent }: ForgotPasswordProps) {
const [email, setEmail] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState(false);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError("");
setIsLoading(true);
try {
const result = await resetPassword({ username: email });
Eif (result.nextStep.resetPasswordStep === "CONFIRM_RESET_PASSWORD_WITH_CODE") {
setSuccess(true);
}
} catch (err: unknown) {
if (err instanceof Error) {
if (err.name === "UserNotFoundException") {
// For security, don't reveal if user exists
// Show success anyway to prevent email enumeration
setSuccess(true);
} else if (err.name === "LimitExceededException") {
setError("Too many attempts. Please try again later.");
} else E{
setError(err.message || "Failed to send reset code. Please try again.");
}
} else E{
setError("Failed to send reset code. Please try again.");
}
} finally {
setIsLoading(false);
}
};
const handleProceedToReset = () => {
onCodeSent?.(email);
};
if (success) {
return (
<Card className="w-full max-w-md mx-auto" data-testid="forgot-password-success">
<CardHeader className="text-center">
<CheckCircle2 className="h-12 w-12 text-green-500 mx-auto mb-4" />
<CardTitle>Check your email</CardTitle>
<CardDescription>
If an account exists for {email}, we've sent a password reset code.
</CardDescription>
</CardHeader>
<CardFooter className="flex flex-col gap-3">
<Button
onClick={handleProceedToReset}
className="w-full"
data-testid="enter-reset-code-button"
>
Enter Reset Code
</Button>
<p className="text-sm text-center text-muted-foreground">
Didn't receive the email? Check your spam folder or try again.
</p>
</CardFooter>
</Card>
);
}
return (
<Card className="w-full max-w-md mx-auto" data-testid="forgot-password-form">
<CardHeader className="text-center">
<div className="flex items-center justify-center gap-2 mb-4">
<Mail className="h-8 w-8 text-primary" />
</div>
<CardTitle>Reset your password</CardTitle>
<CardDescription>
Enter your email address and we'll send you a code to reset your password
</CardDescription>
</CardHeader>
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="email">Email</Label>
<Input
id="email"
type="email"
data-testid="forgot-password-email"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="you@example.com"
required
disabled={isLoading}
autoComplete="email"
/>
</div>
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
</CardContent>
<CardFooter className="flex flex-col gap-4">
<Button
type="submit"
className="w-full"
disabled={isLoading}
data-testid="forgot-password-submit"
>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Send Reset Code
</Button>
<p className="text-sm text-center text-muted-foreground">
Remember your password?{" "}
<a
href="/signin"
className="text-primary hover:underline font-medium"
>
Sign in
</a>
</p>
</CardFooter>
</form>
</Card>
);
}
|