@@ -42,7 +42,7 @@ const Login: NextPageWithLayout = () => { const desktop = device === 'desktop' - const { register, handleSubmit } = useForm() + const { register, handleSubmit, getValues, setValue } = useForm() useEffect(() => { const params = new URL(window.location.href).searchParams @@ -60,7 +60,7 @@ const Login: NextPageWithLayout = () => { const resp = await signIn('credentials', { username: data.email, - password: data.password, + password: data.password1, redirect: false, }) if (resp && !resp.ok && resp.error && resp.error !== '') { @@ -85,9 +85,40 @@ const Login: NextPageWithLayout = () => { } } - const handleInputChangeTrim = (event: any) => { - event.target.value = event.target.value.trim() - } + // Браузерный autofill часто не триггерит onChange, поэтому синхронизируем значения из DOM. + useEffect(() => { + const syncFromDom = () => { + const emailInput = document.querySelector('input[name="email"]') + const passwordInput = document.querySelector('input[name="password1"]') + if (!emailInput && !passwordInput) return + + const domEmail = emailInput?.value?.trim() ?? '' + const domPassword = passwordInput?.value ?? '' + + const current = getValues() + + if (domEmail && domEmail !== (current.email ?? '')) { + setValue('email', domEmail, { shouldDirty: true, shouldValidate: true }) + } + + if (domPassword && domPassword !== (current.password1 ?? '')) { + setValue('password1', domPassword, { shouldDirty: true, shouldValidate: true }) + } + } + + // Сразу после монтирования + короткий период, чтобы поймать delayed autofill. + syncFromDom() + const intervalId = window.setInterval(syncFromDom, 100) + window.setTimeout(() => window.clearInterval(intervalId), 1500) + + const onFocus = () => syncFromDom() + window.addEventListener('focus', onFocus) + + return () => { + window.clearInterval(intervalId) + window.removeEventListener('focus', onFocus) + } + }, [getValues, setValue]) return ( <> @@ -123,7 +154,7 @@ const Login: NextPageWithLayout = () => { marginTop: desktop ? 0 : '10px', }} > -
+ { (typeof v === 'string' ? v.trim() : v), + })} /> { @@ -253,8 +288,8 @@ const Login: NextPageWithLayout = () => { ), }} sx={{ ...InputStyleDark }} - {...register('password', { - ...PasswordOptions, + {...register('password1', { + ...(PasswordOptions as any), })} />