mirror of
https://github.com/fosrl/pangolin.git
synced 2026-06-07 16:18:47 +00:00
improve mfa autofill
This commit is contained in:
@@ -345,6 +345,7 @@ export default function LoginForm({
|
|||||||
error={error}
|
error={error}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
formId="form"
|
formId="form"
|
||||||
|
username={form.getValues("email")}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -265,6 +265,7 @@ export default function LoginPasswordForm({
|
|||||||
}}
|
}}
|
||||||
error={error}
|
error={error}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
|
username={identifier}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useRef } from "react";
|
|
||||||
import { UseFormReturn } from "react-hook-form";
|
import { UseFormReturn } from "react-hook-form";
|
||||||
import { Button } from "@app/components/ui/button";
|
import { Button } from "@app/components/ui/button";
|
||||||
import {
|
import {
|
||||||
@@ -8,17 +7,15 @@ import {
|
|||||||
FormControl,
|
FormControl,
|
||||||
FormField,
|
FormField,
|
||||||
FormItem,
|
FormItem,
|
||||||
|
FormLabel,
|
||||||
FormMessage
|
FormMessage
|
||||||
} from "@app/components/ui/form";
|
} from "@app/components/ui/form";
|
||||||
import {
|
import { InputOTP, InputOTPGroup, InputOTPSlot } from "./ui/input-otp";
|
||||||
InputOTP,
|
|
||||||
InputOTPGroup,
|
|
||||||
InputOTPSlot
|
|
||||||
} from "./ui/input-otp";
|
|
||||||
import { Alert, AlertDescription } from "@app/components/ui/alert";
|
import { Alert, AlertDescription } from "@app/components/ui/alert";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import { REGEXP_ONLY_DIGITS_AND_CHARS } from "input-otp";
|
import { REGEXP_ONLY_DIGITS } from "input-otp";
|
||||||
import * as z from "zod";
|
|
||||||
|
const MFA_OTP_INPUT_ID = "mfa-otp-code";
|
||||||
|
|
||||||
type MfaInputFormProps = {
|
type MfaInputFormProps = {
|
||||||
form: UseFormReturn<{ code: string }>;
|
form: UseFormReturn<{ code: string }>;
|
||||||
@@ -27,6 +24,7 @@ type MfaInputFormProps = {
|
|||||||
error?: string | null;
|
error?: string | null;
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
formId?: string;
|
formId?: string;
|
||||||
|
username?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function MfaInputForm({
|
export default function MfaInputForm({
|
||||||
@@ -35,55 +33,10 @@ export default function MfaInputForm({
|
|||||||
onBack,
|
onBack,
|
||||||
error,
|
error,
|
||||||
loading = false,
|
loading = false,
|
||||||
formId = "mfaForm"
|
formId = "mfaForm",
|
||||||
|
username
|
||||||
}: MfaInputFormProps) {
|
}: MfaInputFormProps) {
|
||||||
const t = useTranslations();
|
const t = useTranslations();
|
||||||
const otpContainerRef = useRef<HTMLDivElement>(null);
|
|
||||||
|
|
||||||
// Auto-focus MFA input when component mounts
|
|
||||||
useEffect(() => {
|
|
||||||
const focusInput = () => {
|
|
||||||
// Try using the ref first
|
|
||||||
if (otpContainerRef.current) {
|
|
||||||
const hiddenInput = otpContainerRef.current.querySelector(
|
|
||||||
"input"
|
|
||||||
) as HTMLInputElement;
|
|
||||||
if (hiddenInput) {
|
|
||||||
hiddenInput.focus();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fallback: query the DOM
|
|
||||||
const otpContainer = document.querySelector(
|
|
||||||
'[data-slot="input-otp"]'
|
|
||||||
);
|
|
||||||
if (!otpContainer) return;
|
|
||||||
|
|
||||||
const hiddenInput = otpContainer.querySelector(
|
|
||||||
"input"
|
|
||||||
) as HTMLInputElement;
|
|
||||||
if (hiddenInput) {
|
|
||||||
hiddenInput.focus();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Last resort: click the first slot
|
|
||||||
const firstSlot = otpContainer.querySelector(
|
|
||||||
'[data-slot="input-otp-slot"]'
|
|
||||||
) as HTMLElement;
|
|
||||||
if (firstSlot) {
|
|
||||||
firstSlot.click();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Use requestAnimationFrame to wait for the next paint
|
|
||||||
requestAnimationFrame(() => {
|
|
||||||
requestAnimationFrame(() => {
|
|
||||||
focusInput();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
@@ -99,25 +52,45 @@ export default function MfaInputForm({
|
|||||||
className="space-y-4"
|
className="space-y-4"
|
||||||
id={formId}
|
id={formId}
|
||||||
>
|
>
|
||||||
|
{username ? (
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
name="username"
|
||||||
|
autoComplete="username"
|
||||||
|
value={username}
|
||||||
|
readOnly
|
||||||
|
tabIndex={-1}
|
||||||
|
aria-hidden="true"
|
||||||
|
className="sr-only"
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="code"
|
name="code"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormControl>
|
<FormLabel
|
||||||
<div
|
htmlFor={MFA_OTP_INPUT_ID}
|
||||||
ref={otpContainerRef}
|
className="sr-only"
|
||||||
className="flex justify-center"
|
|
||||||
>
|
>
|
||||||
|
{t("otpAuth")}
|
||||||
|
</FormLabel>
|
||||||
|
<FormControl>
|
||||||
|
<div className="flex justify-center">
|
||||||
<InputOTP
|
<InputOTP
|
||||||
|
id={MFA_OTP_INPUT_ID}
|
||||||
maxLength={6}
|
maxLength={6}
|
||||||
{...field}
|
{...field}
|
||||||
|
autoComplete="one-time-code"
|
||||||
|
inputMode="numeric"
|
||||||
autoFocus
|
autoFocus
|
||||||
pattern={REGEXP_ONLY_DIGITS_AND_CHARS}
|
pattern={REGEXP_ONLY_DIGITS}
|
||||||
onChange={(value: string) => {
|
onChange={(value: string) => {
|
||||||
field.onChange(value);
|
field.onChange(value);
|
||||||
if (value.length === 6) {
|
if (value.length === 6) {
|
||||||
form.handleSubmit(onSubmit)();
|
form.handleSubmit(
|
||||||
|
onSubmit
|
||||||
|
)();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|||||||
Reference in New Issue
Block a user