feat(ssr): enhance login and signup experience (#3510)
This commit is contained in:
@@ -29,6 +29,27 @@ import { Link, useLocation, useNavigate } from "react-router"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
|
||||
function closeRecaptcha(recaptchaRef: React.RefObject<ReCAPTCHA>, 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<ReCAPTCHA>(null)
|
||||
|
||||
const resetLoadingState = useCallback(() => {
|
||||
setIsButtonLoading(false)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (isButtonLoading) {
|
||||
return closeRecaptcha(recaptchaRef, resetLoadingState)
|
||||
}
|
||||
}, [isButtonLoading, resetLoadingState])
|
||||
|
||||
async function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
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() {
|
||||
<FormItem>
|
||||
<FormLabel>{t("login.email")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="email" {...field} />
|
||||
<Input type="email" {...field} disabled={isButtonLoading || needTwoFactor} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -291,7 +338,7 @@ function LoginWithPassword() {
|
||||
</Link>
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} />
|
||||
<Input type="password" {...field} disabled={isButtonLoading || needTwoFactor} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -305,7 +352,7 @@ function LoginWithPassword() {
|
||||
<FormItem>
|
||||
<FormLabel>{t("login.two_factor.code")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="text" {...field} />
|
||||
<Input type="text" {...field} disabled={isButtonLoading} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -313,8 +360,16 @@ function LoginWithPassword() {
|
||||
/>
|
||||
)}
|
||||
<ReCAPTCHA ref={recaptchaRef} sitekey={env.VITE_RECAPTCHA_V2_SITE_KEY} size="invisible" />
|
||||
<Button type="submit" buttonClassName="!mt-3 w-full" isLoading={isSubmitting} size="lg">
|
||||
{t("login.continueWith", { provider: t("words.email") })}
|
||||
<Button
|
||||
type="submit"
|
||||
buttonClassName="!mt-3 w-full"
|
||||
isLoading={isButtonLoading}
|
||||
size="lg"
|
||||
disabled={isButtonLoading}
|
||||
>
|
||||
{needTwoFactor
|
||||
? t("login.two_factor.verify")
|
||||
: t("login.continueWith", { provider: t("words.email") })}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
@@ -325,6 +380,7 @@ function LoginWithPassword() {
|
||||
navigate("/register")
|
||||
}}
|
||||
size="lg"
|
||||
disabled={isButtonLoading}
|
||||
>
|
||||
{t("login.signUp")}
|
||||
</Button>
|
||||
|
||||
@@ -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<ReCAPTCHA>,
|
||||
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 (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-10">
|
||||
@@ -43,6 +67,9 @@ const formSchema = z
|
||||
|
||||
function RegisterForm() {
|
||||
const { t } = useTranslation()
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const navigate = useNavigate()
|
||||
const recaptchaRef = useRef<ReCAPTCHA>(null)
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
@@ -53,34 +80,48 @@ function RegisterForm() {
|
||||
},
|
||||
})
|
||||
|
||||
const { isValid } = form.formState
|
||||
|
||||
const navigate = useNavigate()
|
||||
|
||||
const recaptchaRef = useRef<ReCAPTCHA>(null)
|
||||
useEffect(() => {
|
||||
if (isSubmitting) {
|
||||
return closeRecaptcha(recaptchaRef, setIsSubmitting)
|
||||
}
|
||||
}, [isSubmitting])
|
||||
|
||||
async function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
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() {
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.email")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="email" {...field} />
|
||||
<Input type="email" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -122,7 +163,7 @@ function RegisterForm() {
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} />
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
@@ -135,14 +176,20 @@ function RegisterForm() {
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.confirm_password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} />
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<ReCAPTCHA ref={recaptchaRef} sitekey={env.VITE_RECAPTCHA_V2_SITE_KEY} size="invisible" />
|
||||
<Button disabled={!isValid} type="submit" className="w-full" size="lg">
|
||||
<Button
|
||||
isLoading={isSubmitting}
|
||||
disabled={isSubmitting}
|
||||
type="submit"
|
||||
className="w-full"
|
||||
size="lg"
|
||||
>
|
||||
{t("register.submit")}
|
||||
</Button>
|
||||
</form>
|
||||
|
||||
Vendored
+2
-1
@@ -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.",
|
||||
|
||||
Vendored
+2
@@ -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}} 并关闭此页面。",
|
||||
|
||||
Reference in New Issue
Block a user