Skip to content

Commit

Permalink
autofill
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 8, 2024
1 parent 5153c7e commit c887bb7
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/components/auth/totpDigits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ export default function TwoFactAuth({
if (inputRefs.current[0]) {
inputRefs.current[0].focus();
}
const handleAutofill = (e: Event) => {
const input = e.target as HTMLInputElement;
if (input.value.length === 6) {
const newDigits = input.value.split("");
updateDigits(newDigits);
input.blur(); // Remove focus to prevent additional autofill attempts
}
};

for (const input of inputRefs.current) {
if (input) {
input.addEventListener("input", handleAutofill);
}
}

return () => {
for (const input of inputRefs.current) {
if (input) {
input.removeEventListener("input", handleAutofill);
}
}
};
}, []);

const updateDigits = (newDigits: string[]) => {
Expand All @@ -45,6 +67,7 @@ export default function TwoFactAuth({
};

const handleChange = (index: number, newDigit: string) => {
console.log(newDigit);
if (/^[0-9]$/.test(newDigit) || newDigit === "") {
const newDigits = [...digits];
newDigits[index] = newDigit;
Expand Down Expand Up @@ -78,6 +101,8 @@ export default function TwoFactAuth({
key={index}
tabIndex={0}
data-testid="totp-input-digit"
name="totp"
pattern="\d*"
// biome-ignore lint/suspicious/noAssignInExpressions: <explanation>
ref={(el) => (inputRefs.current[index] = el)}
className="input input-bordered input-sm w-10 text-center"
Expand Down

0 comments on commit c887bb7

Please sign in to comment.