feat(ssr): optimize login
This commit is contained in:
@@ -16,15 +16,13 @@ import {
|
||||
} from "@follow/components/ui/form/index.jsx"
|
||||
import { Input } from "@follow/components/ui/input/index.js"
|
||||
import { LoadingCircle } from "@follow/components/ui/loading/index.jsx"
|
||||
import { authProvidersConfig } from "@follow/constants"
|
||||
import { DEEPLINK_SCHEME } from "@follow/shared/constants"
|
||||
import { env } from "@follow/shared/env.ssr"
|
||||
import { cn } from "@follow/utils/utils"
|
||||
import { zodResolver } from "@hookform/resolvers/zod"
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react"
|
||||
import ReCAPTCHA from "react-google-recaptcha"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { Trans, useTranslation } from "react-i18next"
|
||||
import { Link, useLocation, useNavigate } from "react-router"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
@@ -98,6 +96,10 @@ export function Login() {
|
||||
onceRef.current = true
|
||||
}, [handleOpenApp, isAuthenticated])
|
||||
|
||||
const navigate = useNavigate()
|
||||
|
||||
const [isEmail, setIsEmail] = useState(false)
|
||||
|
||||
const LoginOrStatusContent = useMemo(() => {
|
||||
switch (true) {
|
||||
case isAuthenticated: {
|
||||
@@ -141,76 +143,69 @@ export function Login() {
|
||||
)
|
||||
}
|
||||
default: {
|
||||
if (!authProviders?.credential) {
|
||||
return (
|
||||
<div className="flex w-[350px] max-w-full flex-col gap-3">
|
||||
{Object.entries(authProviders || [])
|
||||
.filter(([key]) => key !== "credential")
|
||||
.map(([key, provider]) => (
|
||||
<Button
|
||||
key={key}
|
||||
buttonClassName={cn(
|
||||
"h-[48px] w-full rounded-[8px] font-sans text-base text-white hover:!bg-black/80 focus:!border-black/80 focus:!ring-black/80",
|
||||
authProvidersConfig[key]?.buttonClassName,
|
||||
)}
|
||||
onClick={() => {
|
||||
loginHandler(key, "app")
|
||||
}}
|
||||
>
|
||||
<i className={cn("mr-2 text-xl", authProvidersConfig[key]?.iconClassName)} />{" "}
|
||||
{t("login.continueWith", {
|
||||
provider: provider.name,
|
||||
})}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
)
|
||||
} else {
|
||||
return (
|
||||
<>
|
||||
return (
|
||||
<>
|
||||
{isEmail ? (
|
||||
<LoginWithPassword />
|
||||
<div className="mt-2 w-full space-y-2">
|
||||
<div className="flex items-center justify-center">
|
||||
<Divider className="flex-1" />
|
||||
<p className="text-text-secondary px-4 text-center text-sm">{t("login.or")}</p>
|
||||
<Divider className="flex-1" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center justify-center gap-4">
|
||||
{Object.entries(authProviders || [])
|
||||
.filter(([key]) => key !== "credential")
|
||||
.map(([key, provider]) => (
|
||||
<MotionButtonBase
|
||||
key={key}
|
||||
onClick={() => {
|
||||
) : (
|
||||
<div className="mb-3 flex flex-col items-center justify-center gap-4">
|
||||
{Object.entries(authProviders || []).map(([key, provider]) => (
|
||||
<MotionButtonBase
|
||||
key={key}
|
||||
onClick={() => {
|
||||
if (key === "credential") {
|
||||
setIsEmail(true)
|
||||
} else {
|
||||
loginHandler(key, "app")
|
||||
}
|
||||
}}
|
||||
className="center hover:bg-material-medium relative w-full gap-2 rounded-xl border p-2.5 pl-5 font-semibold duration-200"
|
||||
>
|
||||
<img
|
||||
className="absolute left-9 h-5"
|
||||
style={{
|
||||
color: provider.color,
|
||||
}}
|
||||
>
|
||||
<div
|
||||
className="center hover:bg-material-medium inline-flex rounded-full border p-2.5 duration-200 [&_svg]:size-6"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: provider.icon,
|
||||
}}
|
||||
style={{
|
||||
color: provider.color,
|
||||
}}
|
||||
/>
|
||||
</MotionButtonBase>
|
||||
))}
|
||||
src={provider.icon64}
|
||||
/>
|
||||
<span>{t("login.continueWith", { provider: provider.name })}</span>
|
||||
</MotionButtonBase>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)
|
||||
}
|
||||
)}
|
||||
<Divider />
|
||||
{isEmail ? (
|
||||
<div className="cursor-pointer pb-2 text-center" onClick={() => setIsEmail(false)}>
|
||||
Back
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
className="cursor-pointer pb-2 text-center"
|
||||
onClick={() => {
|
||||
navigate("/register")
|
||||
}}
|
||||
>
|
||||
<Trans
|
||||
t={t}
|
||||
i18nKey="login.no_account"
|
||||
components={{
|
||||
strong: <span className="text-accent" />,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
}
|
||||
}, [authProviders, handleOpenApp, isAuthenticated, refetch, t])
|
||||
}, [authProviders, handleOpenApp, isAuthenticated, refetch, t, isEmail, navigate])
|
||||
const Content = useMemo(() => {
|
||||
switch (true) {
|
||||
case redirecting: {
|
||||
return <div className="center">{t("login.redirecting")}</div>
|
||||
}
|
||||
default: {
|
||||
return <div className="flex flex-col gap-3">{LoginOrStatusContent}</div>
|
||||
return <div className="flex min-w-80 flex-col gap-3">{LoginOrStatusContent}</div>
|
||||
}
|
||||
}
|
||||
}, [LoginOrStatusContent, redirecting, t])
|
||||
@@ -248,7 +243,6 @@ function LoginWithPassword() {
|
||||
const [needTwoFactor, setNeedTwoFactor] = useState(false)
|
||||
const [isButtonLoading, setIsButtonLoading] = useState(false)
|
||||
|
||||
const navigate = useNavigate()
|
||||
const recaptchaRef = useRef<ReCAPTCHA>(null)
|
||||
|
||||
const resetLoadingState = useCallback(() => {
|
||||
@@ -374,19 +368,6 @@ function LoginWithPassword() {
|
||||
? t("login.two_factor.verify")
|
||||
: t("login.continueWith", { provider: t("words.email") })}
|
||||
</Button>
|
||||
<Button
|
||||
type="button"
|
||||
buttonClassName="!mt-3"
|
||||
className="w-full"
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
navigate("/register")
|
||||
}}
|
||||
size="lg"
|
||||
disabled={isButtonLoading}
|
||||
>
|
||||
{t("login.signUp")}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { signUp } from "@client/lib/auth"
|
||||
import { loginHandler, signUp } from "@client/lib/auth"
|
||||
import { useAuthProviders } from "@client/query/users"
|
||||
import { Logo } from "@follow/components/icons/logo.jsx"
|
||||
import { Button } from "@follow/components/ui/button/index.jsx"
|
||||
import { Button, MotionButtonBase } from "@follow/components/ui/button/index.jsx"
|
||||
import { Divider } from "@follow/components/ui/divider/index.js"
|
||||
import {
|
||||
Form,
|
||||
FormControl,
|
||||
@@ -17,7 +19,7 @@ import { useEffect, useRef, useState } from "react"
|
||||
import ReCAPTCHA from "react-google-recaptcha"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { Trans, useTranslation } from "react-i18next"
|
||||
import { Link, useNavigate } from "react-router"
|
||||
import { useNavigate } from "react-router"
|
||||
import { toast } from "sonner"
|
||||
import { z } from "zod"
|
||||
|
||||
@@ -47,8 +49,8 @@ function closeRecaptcha(
|
||||
|
||||
export function Component() {
|
||||
return (
|
||||
<div className="flex h-full flex-col items-center justify-center gap-10">
|
||||
<Logo className="size-20" />
|
||||
<div className="flex h-full flex-col items-center justify-center gap-8">
|
||||
<Logo className="size-16" />
|
||||
<RegisterForm />
|
||||
</div>
|
||||
)
|
||||
@@ -80,6 +82,10 @@ function RegisterForm() {
|
||||
},
|
||||
})
|
||||
|
||||
const [isEmail, setIsEmail] = useState(false)
|
||||
|
||||
const { data: authProviders } = useAuthProviders()
|
||||
|
||||
useEffect(() => {
|
||||
if (isSubmitting) {
|
||||
return closeRecaptcha(recaptchaRef, setIsSubmitting)
|
||||
@@ -125,75 +131,115 @@ function RegisterForm() {
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
<h1 className="center flex text-2xl font-bold">
|
||||
{t("register.label", { app_name: APP_NAME })}
|
||||
<div className="relative min-w-80">
|
||||
<h1 className="mb-6 text-center text-2xl">
|
||||
{t("login.signUpTo")} <b>{` ${APP_NAME}`}</b>
|
||||
</h1>
|
||||
<div className="text-text-secondary mt-2 text-center">
|
||||
<Trans
|
||||
i18nKey="register.note"
|
||||
components={{
|
||||
LoginLink: (
|
||||
<Link to="/login" className="text-accent hover:underline">
|
||||
{t("register.login")}
|
||||
</Link>
|
||||
),
|
||||
{isEmail ? (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="mt-6 space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.email")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="email" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="confirmPassword"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.confirm_password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<ReCAPTCHA
|
||||
ref={recaptchaRef}
|
||||
sitekey={env.VITE_RECAPTCHA_V2_SITE_KEY}
|
||||
size="invisible"
|
||||
/>
|
||||
<Button
|
||||
isLoading={isSubmitting}
|
||||
disabled={isSubmitting}
|
||||
type="submit"
|
||||
className="w-full"
|
||||
size="lg"
|
||||
>
|
||||
{t("register.submit")}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
) : (
|
||||
<div className="mb-3 flex flex-col items-center justify-center gap-4">
|
||||
{Object.entries(authProviders || []).map(([key, provider]) => (
|
||||
<MotionButtonBase
|
||||
key={key}
|
||||
onClick={() => {
|
||||
if (key === "credential") {
|
||||
setIsEmail(true)
|
||||
} else {
|
||||
loginHandler(key, "app")
|
||||
}
|
||||
}}
|
||||
className="center hover:bg-material-medium relative w-full gap-2 rounded-xl border p-2.5 pl-5 font-semibold duration-200"
|
||||
>
|
||||
<img
|
||||
className="absolute left-9 h-5"
|
||||
style={{
|
||||
color: provider.color,
|
||||
}}
|
||||
src={provider.icon64}
|
||||
/>
|
||||
<span>{t("login.continueWith", { provider: provider.name })}</span>
|
||||
</MotionButtonBase>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<Divider className="my-7" />
|
||||
{isEmail ? (
|
||||
<div className="cursor-pointer pb-2 text-center" onClick={() => setIsEmail(false)}>
|
||||
Back
|
||||
</div>
|
||||
) : (
|
||||
<div
|
||||
className="cursor-pointer pb-2 text-center"
|
||||
onClick={() => {
|
||||
navigate("/login")
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="mt-6 space-y-4">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.email")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="email" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
>
|
||||
<Trans
|
||||
t={t}
|
||||
i18nKey="login.have_account"
|
||||
components={{
|
||||
strong: <span className="text-accent" />,
|
||||
}}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="password"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="confirmPassword"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>{t("register.confirm_password")}</FormLabel>
|
||||
<FormControl>
|
||||
<Input type="password" {...field} disabled={isSubmitting} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<ReCAPTCHA ref={recaptchaRef} sitekey={env.VITE_RECAPTCHA_V2_SITE_KEY} size="invisible" />
|
||||
<Button
|
||||
isLoading={isSubmitting}
|
||||
disabled={isSubmitting}
|
||||
type="submit"
|
||||
className="w-full"
|
||||
size="lg"
|
||||
>
|
||||
{t("register.submit")}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -62,6 +62,7 @@ export interface AuthProvider {
|
||||
id: string
|
||||
color: string
|
||||
icon: string
|
||||
icon64: string
|
||||
}
|
||||
const getTypedProviders = async () => {
|
||||
const providers = await getProviders()
|
||||
|
||||
@@ -299,7 +299,6 @@
|
||||
"register.confirm_password": "Confirm Password",
|
||||
"register.email": "Email",
|
||||
"register.login": "Login",
|
||||
"register.note": "Already have an account? <LoginLink />",
|
||||
"register.password": "Password",
|
||||
"register.submit": "Create account",
|
||||
"resize.tooltip.double_click_to_collapse": "<b>Double click</b> to collapse",
|
||||
|
||||
@@ -262,7 +262,6 @@
|
||||
"register.confirm_password": "パスワードを確認する",
|
||||
"register.email": "Email",
|
||||
"register.login": "ログイン",
|
||||
"register.note": "既にアカウントをお持ちですか? <LoginLink />",
|
||||
"register.password": "パスワード",
|
||||
"register.submit": "作成",
|
||||
"resize.tooltip.double_click_to_collapse": "<b>ダブルクリック</b> してたたみます",
|
||||
|
||||
@@ -297,7 +297,6 @@
|
||||
"register.confirm_password": "确认密码",
|
||||
"register.email": "邮件地址",
|
||||
"register.login": "登录",
|
||||
"register.note": "已有账户?<LoginLink />",
|
||||
"register.password": "密码",
|
||||
"register.submit": "创建账户",
|
||||
"resize.tooltip.double_click_to_collapse": "<b>双击</b>以折叠",
|
||||
|
||||
@@ -275,7 +275,6 @@
|
||||
"register.confirm_password": "確認密碼",
|
||||
"register.email": "電子信箱",
|
||||
"register.login": "登入",
|
||||
"register.note": "已經有帳號了嗎? <LoginLink />",
|
||||
"register.password": "密碼",
|
||||
"register.submit": "創建帳號",
|
||||
"resize.tooltip.double_click_to_collapse": "<b>雙擊</b>以摺疊",
|
||||
|
||||
Vendored
+3
-1
@@ -48,9 +48,11 @@
|
||||
"login.forget_password.label": "Forget Password",
|
||||
"login.forget_password.note": "Forgot your password?",
|
||||
"login.forget_password.success": "Email has been sent successfully",
|
||||
"login.have_account": "Already have an account? <strong>Sign in →</strong>",
|
||||
"login.logInTo": "Sign in to",
|
||||
"login.logInWithEmail": "Sign in with email",
|
||||
"login.new_password.label": "New Password",
|
||||
"login.no_account": "Don't have an account? <strong>Sign up →</strong>",
|
||||
"login.openApp": "Open App",
|
||||
"login.or": "Or",
|
||||
"login.password": "Password",
|
||||
@@ -61,6 +63,7 @@
|
||||
"login.reset_password.success": "Password has been successfully reset",
|
||||
"login.signOut": "Sign out",
|
||||
"login.signUp": "Sign up with email",
|
||||
"login.signUpTo": "Sign up to",
|
||||
"login.submit": "Submit",
|
||||
"login.two_factor.code": "Two Factor Code",
|
||||
"login.two_factor.verify": "Complete Verification",
|
||||
@@ -73,7 +76,6 @@
|
||||
"register.email": "Email",
|
||||
"register.label": "Sign up to {{app_name}}",
|
||||
"register.login": "Sign in",
|
||||
"register.note": "Already have an account? <LoginLink />",
|
||||
"register.password": "Password",
|
||||
"register.submit": "Create account",
|
||||
"words.email": "Email"
|
||||
|
||||
Vendored
-1
@@ -64,7 +64,6 @@
|
||||
"register.email": "Email",
|
||||
"register.label": "{{app_name}} のアカウントを作成する",
|
||||
"register.login": "ログイン",
|
||||
"register.note": "アカウントを既にお持ちですか? <LoginLink />",
|
||||
"register.password": "パスワード",
|
||||
"register.submit": "アカウントを作成",
|
||||
"words.email": "Email"
|
||||
|
||||
Vendored
+7
-5
@@ -39,7 +39,7 @@
|
||||
"invitation.title": "邀请码",
|
||||
"login.backToWebApp": "返回网页版",
|
||||
"login.confirm_password.label": "确认密码",
|
||||
"login.continueWith": "使用 {{provider}} 登入",
|
||||
"login.continueWith": "使用 {{provider}} 登录",
|
||||
"login.email": "邮件地址",
|
||||
"login.errors.unknown": "未知错误",
|
||||
"login.forget_password.description": "请输入与你的帐户关联的邮件地址,我们将向你发送一封关于如何重置密码的邮件。",
|
||||
@@ -48,9 +48,11 @@
|
||||
"login.forget_password.label": "忘记密码",
|
||||
"login.forget_password.note": "忘记了密码?",
|
||||
"login.forget_password.success": "邮件已成功发送。",
|
||||
"login.logInTo": "登入至 ",
|
||||
"login.logInWithEmail": "使用邮件地址登入",
|
||||
"login.have_account": "已有账户?<strong>登录 →</strong>",
|
||||
"login.logInTo": "登录 ",
|
||||
"login.logInWithEmail": "使用邮件地址登录",
|
||||
"login.new_password.label": "新密码",
|
||||
"login.no_account": "没有账户?<strong>注册 →</strong>",
|
||||
"login.openApp": "打开应用",
|
||||
"login.or": "或",
|
||||
"login.password": "密码",
|
||||
@@ -61,6 +63,7 @@
|
||||
"login.reset_password.success": "密码已成功重置。",
|
||||
"login.signOut": "登出",
|
||||
"login.signUp": "使用邮件地址注册",
|
||||
"login.signUpTo": "注册",
|
||||
"login.submit": "提交",
|
||||
"login.two_factor.code": "双重身份验证码",
|
||||
"login.two_factor.verify": "完成验证",
|
||||
@@ -72,8 +75,7 @@
|
||||
"register.confirm_password": "确认密码",
|
||||
"register.email": "邮件地址",
|
||||
"register.label": "创建 {{app_name}} 账户",
|
||||
"register.login": "登入",
|
||||
"register.note": "已有账户?<LoginLink />",
|
||||
"register.login": "登录",
|
||||
"register.password": "密码",
|
||||
"register.submit": "创建账户",
|
||||
"words.email": "邮件地址"
|
||||
|
||||
Vendored
-1
@@ -65,7 +65,6 @@
|
||||
"register.email": "電子信箱",
|
||||
"register.label": "創建 {{app_name}} 帳號",
|
||||
"register.login": "登入",
|
||||
"register.note": "已經有帳號了嗎? <LoginLink />",
|
||||
"register.password": "密碼",
|
||||
"register.submit": "創建帳號",
|
||||
"words.email": "電子信箱"
|
||||
|
||||
Reference in New Issue
Block a user