diff --git a/shared/index.tsx b/shared/index.tsx index b3959f1d..9f476bf1 100644 --- a/shared/index.tsx +++ b/shared/index.tsx @@ -23,4 +23,4 @@ export { getStorage, setStorage, useStorage } from './manager/storage'; export { setTokenGetter } from './manager/request'; // model -export { loginWithEmail } from './model/user'; +export { loginWithEmail, registerWithEmail } from './model/user'; diff --git a/shared/model/user.ts b/shared/model/user.ts index acb691c7..0b84601b 100644 --- a/shared/model/user.ts +++ b/shared/model/user.ts @@ -1,12 +1,41 @@ import { request } from '../api/request'; +interface UserLoginInfo { + _id: string; + email: string; + password: string; + token: string; + avatar: string | null; + createdAt: string; +} + /** * 邮箱登录 * @param email 邮箱 * @param password 密码 */ -export async function loginWithEmail(email: string, password: string) { - const data = await request.post('/api/user/login', { +export async function loginWithEmail( + email: string, + password: string +): Promise { + const { data } = await request.post('/api/user/login', { + email, + password, + }); + + return data; +} + +/** + * 邮箱注册账号 + * @param email 邮箱 + * @param password 密码 + */ +export async function registerWithEmail( + email: string, + password: string +): Promise { + const { data } = await request.post('/api/user/register', { email, password, }); diff --git a/web/src/routes/Entry/LoginView.tsx b/web/src/routes/Entry/LoginView.tsx index 9043596c..b4b83190 100644 --- a/web/src/routes/Entry/LoginView.tsx +++ b/web/src/routes/Entry/LoginView.tsx @@ -1,9 +1,10 @@ import { Icon } from '@iconify/react'; import { Divider } from 'antd'; import { loginWithEmail, useAsyncFn } from 'pawchat-shared'; -import React, { useState } from 'react'; +import React, { useCallback, useState } from 'react'; import { Spinner } from '../../components/Spinner'; import { string } from 'yup'; +import { useHistory } from 'react-router'; /** * TODO: @@ -28,10 +29,9 @@ OAuthLoginView.displayName = 'OAuthLoginView'; export const LoginView: React.FC = React.memo(() => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); + const history = useHistory(); const [{ loading, error }, handleLogin] = useAsyncFn(async () => { - console.log({ email, password }); - await string() .email('邮箱格式不正确') .required('邮箱不能为空') @@ -43,8 +43,14 @@ export const LoginView: React.FC = React.memo(() => { .validate(password); await loginWithEmail(email, password); + + // TODO }, [email, password]); + const toRegisterView = useCallback(() => { + history.push('/entry/register'); + }, [history]); + return (
登录 Paw Chat
@@ -53,7 +59,7 @@ export const LoginView: React.FC = React.memo(() => {
邮箱
{
密码
{ {error &&

{error.message}

} + +
); diff --git a/web/src/routes/Entry/RegisterView.tsx b/web/src/routes/Entry/RegisterView.tsx new file mode 100644 index 00000000..b8525a98 --- /dev/null +++ b/web/src/routes/Entry/RegisterView.tsx @@ -0,0 +1,88 @@ +import { registerWithEmail, useAsyncFn } from 'pawchat-shared'; +import React, { useCallback, useState } from 'react'; +import { Spinner } from '../../components/Spinner'; +import { string } from 'yup'; +import { Icon } from '@iconify/react'; +import { useHistory } from 'react-router'; + +/** + * 注册视图 + */ +export const RegisterView: React.FC = React.memo(() => { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const history = useHistory(); + + const [{ loading, error }, handleRegister] = useAsyncFn(async () => { + await string() + .email('邮箱格式不正确') + .required('邮箱不能为空') + .validate(email); + + await string() + .min(6, '密码不能低于6位') + .required('密码不能为空') + .validate(password); + + const data = await registerWithEmail(email, password); + + // TODO + console.log(data); + }, [email, password]); + + const toLoginView = useCallback(() => { + history.push('/entry/login'); + }, [history]); + + return ( +
+
注册账号
+ +
+
+
邮箱
+ setEmail(e.target.value)} + /> +
+
+
密码
+ setPassword(e.target.value)} + /> +
+ + {error &&

{error.message}

} + + + + +
+
+ ); +}); +RegisterView.displayName = 'RegisterView'; diff --git a/web/src/routes/Entry/index.tsx b/web/src/routes/Entry/index.tsx index df3f2ef1..834807f5 100644 --- a/web/src/routes/Entry/index.tsx +++ b/web/src/routes/Entry/index.tsx @@ -5,6 +5,7 @@ import bgImage from '../../../assets/images/bg.jpg'; import clsx from 'clsx'; import styles from './index.module.less'; import loginPatternUrl from '../../../assets/images/login-pattern.svg'; +import { RegisterView } from './RegisterView'; export const EntryRoute = React.memo(() => { return ( @@ -18,6 +19,7 @@ export const EntryRoute = React.memo(() => { > +