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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 351x 351x 351x 351x 351x 351x 351x 351x 8x 1x 7x 7x 7x 7x 7x 351x 71x 71x 351x 9x 9x 9x 9x 1x 1x 8x 8x 1x 1x 7x 7x 7x 3x 2x 2x 1x 1x 1x 5x 351x 2x 2x 2x 2x 2x 351x 1x 351x 3x 348x 124x 126x | "use client";
/**
* ResetPassword - Complete password reset with code
* @see tests/components/auth/ResetPassword.test.tsx
* @see JCN-28
*/
import { useState } from "react";
import { confirmResetPassword, 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, KeyRound, CheckCircle2 } from "lucide-react";
interface ResetPasswordProps {
email: string;
onSuccess?: () => void;
}
export function ResetPassword({ email, onSuccess }: ResetPasswordProps) {
const [code, setCode] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState("");
const [success, setSuccess] = useState(false);
const [resendMessage, setResendMessage] = useState("");
const validatePassword = (password: string): string | null => {
if (password.length < 8) {
return "Password must be at least 8 characters";
}
Iif (!/[A-Z]/.test(password)) {
return "Password must contain at least one uppercase letter";
}
Iif (!/[a-z]/.test(password)) {
return "Password must contain at least one lowercase letter";
}
Iif (!/[0-9]/.test(password)) {
return "Password must contain at least one number";
}
Iif (!/[^A-Za-z0-9]/.test(password)) {
return "Password must contain at least one symbol";
}
return null;
};
const handleCodeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value.replace(/[^0-9]/g, "").slice(0, 6);
setCode(value);
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
setError("");
setResendMessage("");
// Validate passwords match
if (newPassword !== confirmPassword) {
setError("Passwords do not match");
return;
}
// Validate password strength
const passwordError = validatePassword(newPassword);
if (passwordError) {
setError(passwordError);
return;
}
setIsLoading(true);
try {
await confirmResetPassword({
username: email,
confirmationCode: code,
newPassword,
});
setSuccess(true);
} catch (err: unknown) {
if (err instanceof Error) {
if (err.name === "CodeMismatchException") {
setError("Invalid verification code. Please check and try again.");
} else if (err.name === "ExpiredCodeException") {
setError("This code has expired. Please request a new one.");
} else E{
setError(err.message || "Failed to reset password. Please try again.");
}
} else E{
setError("Failed to reset password. Please try again.");
}
} finally {
setIsLoading(false);
}
};
const handleResendCode = async () => {
setError("");
setResendMessage("");
try {
await resetPassword({ username: email });
setResendMessage("Code sent! Check your email.");
} catch {
setError("Failed to resend code. Please try again.");
}
};
const handleSignIn = () => {
onSuccess?.();
};
if (success) {
return (
<Card className="w-full max-w-md mx-auto" data-testid="reset-password-success">
<CardHeader className="text-center">
<CheckCircle2 className="h-12 w-12 text-green-500 mx-auto mb-4" />
<CardTitle>Password Reset</CardTitle>
<CardDescription>
Your password has been successfully reset.
</CardDescription>
</CardHeader>
<CardFooter>
<Button
onClick={handleSignIn}
className="w-full"
data-testid="sign-in-after-reset"
>
Sign In
</Button>
</CardFooter>
</Card>
);
}
return (
<Card className="w-full max-w-md mx-auto" data-testid="reset-password-form">
<CardHeader className="text-center">
<div className="flex items-center justify-center gap-2 mb-4">
<KeyRound className="h-8 w-8 text-primary" />
</div>
<CardTitle>Reset your password</CardTitle>
<CardDescription>
Enter the code sent to <span className="font-medium">{email}</span>
</CardDescription>
</CardHeader>
<form onSubmit={handleSubmit}>
<CardContent className="space-y-4">
<div className="space-y-2">
<Label htmlFor="reset-code">Verification Code</Label>
<Input
id="reset-code"
type="text"
inputMode="numeric"
data-testid="reset-code-input"
value={code}
onChange={handleCodeChange}
placeholder="123456"
required
disabled={isLoading}
autoComplete="one-time-code"
className="text-center text-2xl tracking-widest"
/>
</div>
<div className="space-y-2">
<Label htmlFor="new-password">New Password</Label>
<Input
id="new-password"
type="password"
data-testid="reset-new-password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
placeholder="Enter new password"
required
disabled={isLoading}
autoComplete="new-password"
/>
<p className="text-xs text-muted-foreground">
Password must be at least 8 characters with uppercase, lowercase, number, and symbol.
</p>
</div>
<div className="space-y-2">
<Label htmlFor="confirm-password">Confirm Password</Label>
<Input
id="confirm-password"
type="password"
data-testid="reset-confirm-password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
placeholder="Confirm new password"
required
disabled={isLoading}
autoComplete="new-password"
/>
</div>
{error && (
<Alert variant="destructive">
<AlertCircle className="h-4 w-4" />
<AlertTitle>Error</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
{resendMessage && (
<Alert>
<CheckCircle2 className="h-4 w-4" />
<AlertDescription>{resendMessage}</AlertDescription>
</Alert>
)}
</CardContent>
<CardFooter className="flex flex-col gap-4">
<Button
type="submit"
className="w-full"
disabled={isLoading}
data-testid="reset-password-submit"
>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Reset Password
</Button>
<Button
type="button"
variant="ghost"
className="w-full"
onClick={handleResendCode}
disabled={isLoading}
data-testid="resend-reset-code"
>
Resend Code
</Button>
</CardFooter>
</form>
</Card>
);
}
|