diff --git a/web/src/routes/Entry/LoginView.tsx b/web/src/routes/Entry/LoginView.tsx index dd9a17e8..91683b7d 100644 --- a/web/src/routes/Entry/LoginView.tsx +++ b/web/src/routes/Entry/LoginView.tsx @@ -1,12 +1,13 @@ import { Icon } from '@iconify/react'; import { Divider } from 'antd'; -import { loginWithEmail, t, useAsyncFn } from 'tailchat-shared'; +import { isValidStr, loginWithEmail, t, useAsyncFn } from 'tailchat-shared'; import React, { useCallback, useState } from 'react'; import { Spinner } from '../../components/Spinner'; import { string } from 'yup'; import { useHistory } from 'react-router'; import { setUserJWT } from '../../utils/jwt-helper'; import { setGlobalUserLoginInfo } from '../../utils/user-helper'; +import { useSearchParam } from '@/hooks/useSearchParam'; /** * TODO: @@ -15,7 +16,7 @@ import { setGlobalUserLoginInfo } from '../../utils/user-helper'; const OAuthLoginView: React.FC = React.memo(() => { return ( <> - + {t('或')}
@@ -32,32 +33,42 @@ export const LoginView: React.FC = React.memo(() => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const history = useHistory(); + const navRedirect = useSearchParam('redirect'); const [{ loading, error }, handleLogin] = useAsyncFn(async () => { await string() - .email('邮箱格式不正确') - .required('邮箱不能为空') + .email(t('邮箱格式不正确')) + .required(t('邮箱不能为空')) .validate(email); await string() - .min(6, '密码不能低于6位') - .required('密码不能为空') + .min(6, t('密码不能低于6位')) + .required(t('密码不能为空')) .validate(password); const data = await loginWithEmail(email, password); setGlobalUserLoginInfo(data); await setUserJWT(data.token); - history.push('/main'); - }, [email, password, history]); + + if (isValidStr(navRedirect)) { + history.push(decodeURIComponent(navRedirect)); + } else { + history.push('/main'); + } + }, [email, password, history, navRedirect]); const toRegisterView = useCallback(() => { - history.push('/entry/register'); + // 携带上下文切换路由 + history.push({ + ...history.location, + pathname: '/entry/register', + }); }, [history]); return (
-
登录 Tail Chat
+
{t('登录 Tail Chat')}
@@ -72,7 +83,7 @@ export const LoginView: React.FC = React.memo(() => { />
-
密码
+
{t('密码')}
{ onClick={handleLogin} > {loading && } - 登录 + {t('登录')}
diff --git a/web/src/routes/Entry/RegisterView.tsx b/web/src/routes/Entry/RegisterView.tsx index 9e7596da..c74e5807 100644 --- a/web/src/routes/Entry/RegisterView.tsx +++ b/web/src/routes/Entry/RegisterView.tsx @@ -1,4 +1,4 @@ -import { registerWithEmail, useAsyncFn } from 'tailchat-shared'; +import { isValidStr, registerWithEmail, t, useAsyncFn } from 'tailchat-shared'; import React, { useCallback, useState } from 'react'; import { Spinner } from '../../components/Spinner'; import { string } from 'yup'; @@ -7,7 +7,6 @@ import { useHistory } from 'react-router'; import { setUserJWT } from '../../utils/jwt-helper'; import { setGlobalUserLoginInfo } from '../../utils/user-helper'; import { useSearchParam } from '@/hooks/useSearchParam'; -import { isValidStr } from '../../../../shared/utils/string-helper'; /** * 注册视图 @@ -20,13 +19,13 @@ export const RegisterView: React.FC = React.memo(() => { const [{ loading, error }, handleRegister] = useAsyncFn(async () => { await string() - .email('邮箱格式不正确') - .required('邮箱不能为空') + .email(t('邮箱格式不正确')) + .required(t('邮箱不能为空')) .validate(email); await string() - .min(6, '密码不能低于6位') - .required('密码不能为空') + .min(6, t('密码不能低于6位')) + .required(t('密码不能为空')) .validate(password); const data = await registerWithEmail(email, password); @@ -42,16 +41,20 @@ export const RegisterView: React.FC = React.memo(() => { }, [email, password, navRedirect]); const toLoginView = useCallback(() => { - history.push('/entry/login'); + // 携带上下文切换路由 + history.push({ + ...history.location, + pathname: '/entry/login', + }); }, [history]); return (
-
注册账号
+
{t('注册账号')}
-
邮箱
+
{t('邮箱')}
{ />
-
密码
+
{t('密码')}
{ onClick={handleRegister} > {loading && } - 注册账号 + {t('注册账号')}