diff --git a/apps/ssr/client/modules/login/index.tsx b/apps/ssr/client/modules/login/index.tsx index 2d8667a70..04c9e7577 100644 --- a/apps/ssr/client/modules/login/index.tsx +++ b/apps/ssr/client/modules/login/index.tsx @@ -29,6 +29,27 @@ import { Link, useLocation, useNavigate } from "react-router" import { toast } from "sonner" import { z } from "zod" +function closeRecaptcha(recaptchaRef: React.RefObject, resetLoadingState: () => void) { + const handleClick = (e: MouseEvent) => { + const recaptchaIframeSelector = + 'iframe[src*="recaptcha/api2"], iframe[src*="www.recaptcha.net"], iframe[src*="google.com/recaptcha"]' + const recaptchaChallengeIframe = document.querySelector(recaptchaIframeSelector) + + if ( + e.target instanceof Element && + recaptchaChallengeIframe && + !recaptchaChallengeIframe.contains(e.target) && + !e.target.closest(".g-recaptcha") + ) { + recaptchaRef.current?.reset() + resetLoadingState() + } + } + + document.addEventListener("click", handleClick) + return () => document.removeEventListener("click", handleClick) +} + export function Login() { const { status, refetch } = useSession() @@ -222,41 +243,67 @@ function LoginWithPassword() { }, }) const [needTwoFactor, setNeedTwoFactor] = useState(false) + const [isButtonLoading, setIsButtonLoading] = useState(false) - const { isSubmitting } = form.formState const navigate = useNavigate() - const recaptchaRef = useRef(null) + const resetLoadingState = useCallback(() => { + setIsButtonLoading(false) + }, []) + + useEffect(() => { + if (isButtonLoading) { + return closeRecaptcha(recaptchaRef, resetLoadingState) + } + }, [isButtonLoading, resetLoadingState]) + async function onSubmit(values: z.infer) { - if (needTwoFactor && values.code) { - const res = await twoFactor.verifyTotp({ code: values.code }) + setIsButtonLoading(true) + try { + if (needTwoFactor && values.code) { + const res = await twoFactor.verifyTotp({ code: values.code }) + if (res?.error) { + toast.error(res.error.message) + setIsButtonLoading(false) + } else { + queryClient.invalidateQueries({ queryKey: ["auth", "session"] }) + } + return + } + + const token = await recaptchaRef.current?.executeAsync() + if (!token) { + setIsButtonLoading(false) + return + } + + const res = await loginHandler("credential", "app", { + ...values, + headers: { + "x-token": `r2:${token}`, + }, + }) + if (res?.error) { toast.error(res.error.message) + setIsButtonLoading(false) + return + } + + if ((res?.data as any)?.twoFactorRedirect) { + setNeedTwoFactor(true) + form.setValue("code", "") + setTimeout(() => form.setFocus("code"), 0) + setIsButtonLoading(false) + return } else { queryClient.invalidateQueries({ queryKey: ["auth", "session"] }) } - return - } - - const token = await recaptchaRef.current?.executeAsync() - const res = await loginHandler("credential", "app", { - ...values, - headers: { - "x-token": `r2:${token}`, - }, - }) - if (res?.error) { - toast.error(res.error.message) - return - } - if ((res?.data as any)?.twoFactorRedirect) { - setNeedTwoFactor(true) - form.setValue("code", "") - form.setFocus("code") - return - } else { - queryClient.invalidateQueries({ queryKey: ["auth", "session"] }) + } catch (error) { + console.error("Login error:", error) + toast.error(t("login.errors.unknown")) + setIsButtonLoading(false) } } @@ -270,7 +317,7 @@ function LoginWithPassword() { {t("login.email")} - + @@ -291,7 +338,7 @@ function LoginWithPassword() { - + @@ -305,7 +352,7 @@ function LoginWithPassword() { {t("login.two_factor.code")} - + @@ -313,8 +360,16 @@ function LoginWithPassword() { /> )} - diff --git a/apps/ssr/client/pages/(login)/register.tsx b/apps/ssr/client/pages/(login)/register.tsx index 26b196e35..d8d7a4b7d 100644 --- a/apps/ssr/client/pages/(login)/register.tsx +++ b/apps/ssr/client/pages/(login)/register.tsx @@ -13,7 +13,7 @@ import { Input } from "@follow/components/ui/input/index.js" import { env } from "@follow/shared/env.ssr" import { tracker } from "@follow/tracker" import { zodResolver } from "@hookform/resolvers/zod" -import { useRef } from "react" +import { useEffect, useRef, useState } from "react" import ReCAPTCHA from "react-google-recaptcha" import { useForm } from "react-hook-form" import { Trans, useTranslation } from "react-i18next" @@ -21,6 +21,30 @@ import { Link, useNavigate } from "react-router" import { toast } from "sonner" import { z } from "zod" +function closeRecaptcha( + recaptchaRef: React.RefObject, + setIsSubmitting: (value: boolean) => void, +) { + const handleClick = (e: MouseEvent) => { + const recaptchaIframeSelector = + 'iframe[src*="recaptcha/api2"], iframe[src*="www.recaptcha.net"], iframe[src*="google.com/recaptcha"]' + const recaptchaChallengeIframe = document.querySelector(recaptchaIframeSelector) + + if ( + e.target instanceof Element && + recaptchaChallengeIframe && + !recaptchaChallengeIframe.contains(e.target) && + !e.target.closest(".g-recaptcha") + ) { + recaptchaRef.current?.reset() + setIsSubmitting(false) + } + } + + document.addEventListener("click", handleClick) + return () => document.removeEventListener("click", handleClick) +} + export function Component() { return (
@@ -43,6 +67,9 @@ const formSchema = z function RegisterForm() { const { t } = useTranslation() + const [isSubmitting, setIsSubmitting] = useState(false) + const navigate = useNavigate() + const recaptchaRef = useRef(null) const form = useForm>({ resolver: zodResolver(formSchema), @@ -53,34 +80,48 @@ function RegisterForm() { }, }) - const { isValid } = form.formState - - const navigate = useNavigate() - - const recaptchaRef = useRef(null) + useEffect(() => { + if (isSubmitting) { + return closeRecaptcha(recaptchaRef, setIsSubmitting) + } + }, [isSubmitting]) async function onSubmit(values: z.infer) { - const token = await recaptchaRef.current?.executeAsync() - return signUp.email({ - email: values.email, - password: values.password, - name: values.email.split("@")[0]!, - callbackURL: "/", - fetchOptions: { - onSuccess() { - tracker.register({ - type: "email", - }) - navigate("/login") + setIsSubmitting(true) + + try { + const token = await recaptchaRef.current?.executeAsync() + + if (!token) { + return + } + + await signUp.email({ + email: values.email, + password: values.password, + name: values.email.split("@")[0]!, + callbackURL: "/", + fetchOptions: { + onSuccess() { + tracker.register({ + type: "email", + }) + navigate("/login") + }, + onError(context) { + toast.error(context.error.message) + }, + headers: { + "x-token": `r2:${token}`, + }, }, - onError(context) { - toast.error(context.error.message) - }, - headers: { - "x-token": `r2:${token}`, - }, - }, - }) + }) + } finally { + if (recaptchaRef.current) { + recaptchaRef.current.reset() + } + setIsSubmitting(false) + } } return ( @@ -109,7 +150,7 @@ function RegisterForm() { {t("register.email")} - + @@ -122,7 +163,7 @@ function RegisterForm() { {t("register.password")} - + @@ -135,14 +176,20 @@ function RegisterForm() { {t("register.confirm_password")} - + )} /> - diff --git a/locales/external/en.json b/locales/external/en.json index 257721f95..1ae0afa93 100644 --- a/locales/external/en.json +++ b/locales/external/en.json @@ -38,12 +38,12 @@ "login.confirm_password.label": "Confirm Password", "login.continueWith": "Continue with {{provider}}", "login.email": "Email", + "login.errors.unknown": "Errors Unknown", "login.forget_password.description": "Enter the email address associated with your account and we’ll send you an email about how to reset your password.", "login.forget_password.email_invalid": "Invalid email", "login.forget_password.email_required": "Email required", "login.forget_password.label": "Forget Password", "login.forget_password.note": "Forgot your password?", - "login.forget_password.recaptcha_required": "Please complete the reCAPTCHA verification", "login.forget_password.success": "Email has been sent successfully", "login.logInTo": "Sign in to", "login.logInWithEmail": "Sign in with email", @@ -60,6 +60,7 @@ "login.signUp": "Sign up with email", "login.submit": "Submit", "login.two_factor.code": "Two Factor Code", + "login.two_factor.verify": "Complete Verification", "login.welcomeTo": "Welcome to ", "redirect.continueInBrowser": "Continue in Browser", "redirect.instruction": "Now is the time to open {{app_name}} and safely close this page.", diff --git a/locales/external/zh-CN.json b/locales/external/zh-CN.json index ca542587b..5ecbf1105 100644 --- a/locales/external/zh-CN.json +++ b/locales/external/zh-CN.json @@ -38,6 +38,7 @@ "login.confirm_password.label": "确认密码", "login.continueWith": "使用 {{provider}} 登入", "login.email": "邮件地址", + "login.errors.unknown": "未知错误", "login.forget_password.description": "请输入与你的帐户关联的邮件地址,我们将向你发送一封关于如何重置密码的邮件。", "login.forget_password.email_invalid": "无效的邮箱地址", "login.forget_password.email_required": "请输入邮箱", @@ -59,6 +60,7 @@ "login.signUp": "使用邮件地址注册", "login.submit": "提交", "login.two_factor.code": "双重身份验证码", + "login.two_factor.verify": "完成验证", "login.welcomeTo": "欢迎来到 ", "redirect.continueInBrowser": "在浏览器中继续", "redirect.instruction": "现在可以打开 {{app_name}} 并关闭此页面。",