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 | 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 71x 32x 29x 29x 8x 8x 29x 3x 3x 3x 3x 3x 3x 71x 32x 29x 11x 1x 1x 29x 29x 71x 9x 5x 5x 5x 5x 5x 5x 5x 1x 1x 4x 2x 2x 71x 8x 8x 8x 1x 1x 7x 1x 1x 6x 1x 1x 5x 1x 1x 4x 4x 4x 2x 2x 2x 3x 8x 1x 4x 71x 65x 71x 71x 5x 9x 9x 1x | import { useState, useEffect, useRef, useCallback, useId, type FormEvent } from "react";
import { Button } from "../ui/button";
import { Input } from "../ui/input";
import { PasswordStrength } from "../ui/password-strength";
import { authApi, getErrorMessage } from "../../lib/api";
import { isStrongPassword } from "../../lib/validation";
import { notify } from "../../lib/notify";
import { Lock, X } from "lucide-react";
interface ChangePasswordDialogProps {
open: boolean;
onClose: () => void;
hasPassword: boolean;
onPasswordSet?: () => void;
}
export function ChangePasswordDialog({ open, onClose, hasPassword, onPasswordSet }: ChangePasswordDialogProps) {
const dialogRef = useRef<HTMLDivElement>(null);
const previousFocusRef = useRef<HTMLElement | null>(null);
const titleId = useId();
const [currentPassword, setCurrentPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
const [confirmPassword, setConfirmPassword] = useState("");
const [isChanging, setIsChanging] = useState(false);
const [error, setError] = useState<string | null>(null);
const [touched, setTouched] = useState({ confirm: false });
const confirmError =
touched.confirm && confirmPassword.length > 0 && newPassword !== confirmPassword
? "Passwords do not match"
: undefined;
// Focus management and reset on close
useEffect(() => {
if (open) {
previousFocusRef.current = document.activeElement as HTMLElement;
const timer = setTimeout(() => {
const firstInput = dialogRef.current?.querySelector<HTMLInputElement>("input");
firstInput?.focus();
}, 0);
return () => clearTimeout(timer);
}
// Reset form when dialog closes
setCurrentPassword("");
setNewPassword("");
setConfirmPassword("");
setError(null);
setTouched({ confirm: false });
previousFocusRef.current?.focus();
}, [open]);
// Escape key
useEffect(() => {
if (!open) return;
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
e.stopPropagation();
onClose();
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [open, onClose]);
// Focus trap
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
if (e.key !== "Tab") return;
const dialog = dialogRef.current;
Iif (!dialog) return;
const focusable = dialog.querySelectorAll<HTMLElement>(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])',
);
Iif (focusable.length === 0) return;
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (e.shiftKey && document.activeElement === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && document.activeElement === last) {
e.preventDefault();
first.focus();
}
}, []);
const handleSubmit = async (e: FormEvent) => {
e.preventDefault();
setError(null);
if (hasPassword && !currentPassword) {
setError("Current password is required");
return;
}
if (!isStrongPassword(newPassword)) {
setError("Please meet all password requirements");
return;
}
if (hasPassword && newPassword === currentPassword) {
setError("New password must be different from current password");
return;
}
if (newPassword !== confirmPassword) {
setTouched({ confirm: true });
return;
}
setIsChanging(true);
try {
if (hasPassword) {
await authApi.changePassword(currentPassword, newPassword);
} else {
await authApi.setPassword(newPassword);
onPasswordSet?.();
}
notify.success(hasPassword ? "Password changed successfully" : "Password set successfully");
setTimeout(onClose, 1500);
} catch (err) {
setError(getErrorMessage(err));
} finally {
setIsChanging(false);
}
};
if (!open) return null;
const title = hasPassword ? "Change Password" : "Set Password";
const submitLabel = hasPassword ? "Change Password" : "Set Password";
return (
<div className="fixed inset-0 z-[60] flex items-center justify-center">
{/* Backdrop */}
<button
type="button"
aria-label="Close dialog"
className="fixed inset-0 bg-black/50 cursor-default"
onClick={onClose}
tabIndex={-1}
/>
{/* Dialog */}
<div
ref={dialogRef}
role="dialog"
aria-modal="true"
aria-labelledby={titleId}
className="relative z-10 bg-background-elevated rounded-lg shadow-xl w-full max-w-md mx-4 p-6"
onKeyDown={handleKeyDown}
>
<div className="flex items-center justify-between mb-4">
<h2
id={titleId}
className="text-lg font-semibold text-foreground-default flex items-center gap-2"
>
<Lock className="h-5 w-5" />
{title}
</h2>
<button
type="button"
onClick={onClose}
className="p-1 text-foreground-muted hover:text-foreground-default transition-colors"
aria-label="Close"
>
<X className="h-5 w-5" />
</button>
</div>
{!hasPassword && (
<p className="text-sm text-muted-foreground mb-4">
You signed up without a password. Set one to enable email/password sign-in.
</p>
)}
<form onSubmit={handleSubmit} className="space-y-4">
{hasPassword && (
<div className="space-y-2">
<label
htmlFor="cp-current-password"
className="text-sm font-medium text-foreground-default"
>
Current Password
</label>
<Input
id="cp-current-password"
type="password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
placeholder="Enter current password"
/>
</div>
)}
<div className="space-y-2">
<label
htmlFor="cp-new-password"
className="text-sm font-medium text-foreground-default"
>
New Password
</label>
<Input
id="cp-new-password"
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
placeholder="Enter new password"
/>
<PasswordStrength password={newPassword} />
</div>
<div className="space-y-2">
<label
htmlFor="cp-confirm-password"
className="text-sm font-medium text-foreground-default"
>
Confirm New Password
</label>
<Input
id="cp-confirm-password"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
onBlur={() => setTouched((t) => ({ ...t, confirm: true }))}
placeholder="Confirm new password"
error={confirmError}
/>
</div>
{error && (
<p role="alert" className="text-sm text-destructive">{error}</p>
)}
<div className="flex justify-end gap-3 pt-2">
<Button type="button" variant="outline" onClick={onClose}>
Cancel
</Button>
<Button type="submit" isLoading={isChanging}>
<Lock className="h-4 w-4 mr-2" />
{submitLabel}
</Button>
</div>
</form>
</div>
</div>
);
}
|