更改校验
This commit is contained in:
parent
e5ffe96294
commit
51e86f469b
@ -28,6 +28,5 @@ export async function register(email: string, username: string, password: string
|
|||||||
if (!v.ok) {
|
if (!v.ok) {
|
||||||
throw data
|
throw data
|
||||||
}
|
}
|
||||||
return data as tokenData
|
return
|
||||||
|
|
||||||
}
|
}
|
15
frontend/src/components/Loading.tsx
Normal file
15
frontend/src/components/Loading.tsx
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import Backdrop from "@mui/material/Backdrop";
|
||||||
|
import CircularProgress from "@mui/material/CircularProgress";
|
||||||
|
|
||||||
|
export default function Loading() {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Backdrop
|
||||||
|
sx={{ color: '#fff', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
||||||
|
open={true}
|
||||||
|
>
|
||||||
|
<CircularProgress color="inherit" />
|
||||||
|
</Backdrop>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
@ -11,49 +11,35 @@ import Typography from '@mui/material/Typography';
|
|||||||
import Container from '@mui/material/Container';
|
import Container from '@mui/material/Container';
|
||||||
import Snackbar from '@mui/material/Snackbar';
|
import Snackbar from '@mui/material/Snackbar';
|
||||||
import Alert from '@mui/material/Alert';
|
import Alert from '@mui/material/Alert';
|
||||||
import Backdrop from '@mui/material/Backdrop';
|
|
||||||
import CircularProgress from '@mui/material/CircularProgress';
|
|
||||||
import { useSetAtom } from 'jotai';
|
import { useSetAtom } from 'jotai';
|
||||||
import { token, username } from '@/store/store'
|
import { token, username } from '@/store/store'
|
||||||
import { login } from '@/apis/apis'
|
import { login } from '@/apis/apis'
|
||||||
import { checkEmail } from '@/utils/email'
|
|
||||||
import { Link as RouterLink } from "react-router-dom";
|
import { Link as RouterLink } from "react-router-dom";
|
||||||
|
import Loading from '@/components/Loading'
|
||||||
function Loading() {
|
import CheckInput, { refType } from '@/components/CheckInput'
|
||||||
return (
|
|
||||||
<>
|
|
||||||
<Backdrop
|
|
||||||
sx={{ color: '#fff', zIndex: (theme) => theme.zIndex.drawer + 1 }}
|
|
||||||
open={true}
|
|
||||||
>
|
|
||||||
<CircularProgress color="inherit" />
|
|
||||||
</Backdrop>
|
|
||||||
</>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default function SignIn() {
|
export default function SignIn() {
|
||||||
const [emailErr, setEmailErr] = useState("");
|
|
||||||
const [err, setErr] = useState("");
|
const [err, setErr] = useState("");
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const setToken = useSetAtom(token)
|
const setToken = useSetAtom(token)
|
||||||
const setUsername = useSetAtom(username)
|
const setUsername = useSetAtom(username)
|
||||||
|
const checkList = React.useRef<Map<string, refType>>(new Map<string, refType>())
|
||||||
|
|
||||||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
if (loading) return
|
||||||
|
setLoading(true)
|
||||||
|
|
||||||
const data = new FormData(event.currentTarget);
|
const data = new FormData(event.currentTarget);
|
||||||
const postData = {
|
const postData = {
|
||||||
email: data.get('email')?.toString(),
|
email: data.get('email')?.toString(),
|
||||||
password: data.get('password')?.toString(),
|
password: data.get('password')?.toString(),
|
||||||
}
|
}
|
||||||
if (!checkEmail(postData.email ?? "")) {
|
if (!Array.from(checkList.current.values()).every(v => v.verify())) {
|
||||||
setEmailErr("需要为邮箱")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (loading) return
|
|
||||||
setLoading(true)
|
|
||||||
login(postData.email!, postData.password ?? "").
|
login(postData.email!, postData.password ?? "").
|
||||||
then(v => {
|
then(v => {
|
||||||
if (!v) return
|
if (!v) return
|
||||||
@ -83,9 +69,16 @@ export default function SignIn() {
|
|||||||
登录
|
登录
|
||||||
</Typography>
|
</Typography>
|
||||||
<Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}>
|
<Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}>
|
||||||
<TextField
|
<CheckInput
|
||||||
error={emailErr != ""}
|
ref={(dom) => {
|
||||||
helperText={emailErr}
|
dom && checkList.current.set("1", dom)
|
||||||
|
}}
|
||||||
|
checkList={[
|
||||||
|
{
|
||||||
|
errMsg: "需为邮箱",
|
||||||
|
reg: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
|
||||||
|
}
|
||||||
|
]}
|
||||||
margin="normal"
|
margin="normal"
|
||||||
fullWidth
|
fullWidth
|
||||||
id="email"
|
id="email"
|
||||||
|
@ -2,7 +2,6 @@ import * as React from 'react';
|
|||||||
import Avatar from '@mui/material/Avatar';
|
import Avatar from '@mui/material/Avatar';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import CssBaseline from '@mui/material/CssBaseline';
|
import CssBaseline from '@mui/material/CssBaseline';
|
||||||
import TextField from '@mui/material/TextField';
|
|
||||||
import Link from '@mui/material/Link';
|
import Link from '@mui/material/Link';
|
||||||
import Grid from '@mui/material/Grid';
|
import Grid from '@mui/material/Grid';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
@ -12,22 +11,37 @@ import Container from '@mui/material/Container';
|
|||||||
import { Link as RouterLink } from "react-router-dom";
|
import { Link as RouterLink } from "react-router-dom";
|
||||||
import { register } from '@/apis/apis'
|
import { register } from '@/apis/apis'
|
||||||
import CheckInput, { refType } from '@/components/CheckInput'
|
import CheckInput, { refType } from '@/components/CheckInput'
|
||||||
|
import { useState } from 'react';
|
||||||
|
import Alert from '@mui/material/Alert';
|
||||||
|
import Snackbar from '@mui/material/Snackbar';
|
||||||
|
import Loading from '@/components/Loading'
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
export default function SignUp() {
|
export default function SignUp() {
|
||||||
const checkList = React.useRef<{ [id: string]: refType }>({})
|
const [regErr, setRegErr] = useState("");
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
|
||||||
|
const checkList = React.useRef<Map<string, refType>>(new Map<string, refType>())
|
||||||
|
|
||||||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
if (loading) return
|
||||||
|
setLoading(true)
|
||||||
const data = new FormData(event.currentTarget);
|
const data = new FormData(event.currentTarget);
|
||||||
const d = {
|
const d = {
|
||||||
email: data.get('email')?.toString(),
|
email: data.get('email')?.toString(),
|
||||||
password: data.get('password')?.toString(),
|
password: data.get('password')?.toString(),
|
||||||
password1: data.get('password1')?.toString(),
|
|
||||||
username: data.get("username")?.toString()
|
username: data.get("username")?.toString()
|
||||||
}
|
}
|
||||||
console.log(Object.values(checkList.current).every(v => v.verify()))
|
if (!Array.from(checkList.current.values()).every(v => v.verify())) {
|
||||||
|
return
|
||||||
register(d.email ?? "", d.username ?? "", d.password ?? "")
|
}
|
||||||
|
register(d.email ?? "", d.username ?? "", d.password ?? "").
|
||||||
|
then(() => navigate("/login")).
|
||||||
|
catch(v => [setRegErr(String(v)), console.warn(v)]).
|
||||||
|
finally(() => setLoading(false))
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -54,7 +68,7 @@ export default function SignUp() {
|
|||||||
checkList={[
|
checkList={[
|
||||||
{
|
{
|
||||||
errMsg: "需为邮箱",
|
errMsg: "需为邮箱",
|
||||||
reg: /^hello/
|
reg: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
|
||||||
}
|
}
|
||||||
]}
|
]}
|
||||||
required
|
required
|
||||||
@ -63,21 +77,39 @@ export default function SignUp() {
|
|||||||
label="邮箱"
|
label="邮箱"
|
||||||
autoComplete="email"
|
autoComplete="email"
|
||||||
ref={(dom) => {
|
ref={(dom) => {
|
||||||
dom && [checkList.current["1"] = dom]
|
dom && checkList.current.set("1", dom)
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<TextField
|
<CheckInput
|
||||||
|
ref={(dom) => {
|
||||||
|
dom && checkList.current.set("2", dom)
|
||||||
|
}}
|
||||||
|
checkList={[
|
||||||
|
{
|
||||||
|
errMsg: "长度在 3-16 之间",
|
||||||
|
reg: /.{3,16}/
|
||||||
|
}
|
||||||
|
]}
|
||||||
required
|
required
|
||||||
fullWidth
|
fullWidth
|
||||||
name="username"
|
name="username"
|
||||||
label="角色名"
|
label="角色名"
|
||||||
autoComplete="email"
|
autoComplete="username"
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
<Grid item xs={12}>
|
||||||
<TextField
|
<CheckInput
|
||||||
|
ref={(dom) => {
|
||||||
|
dom && checkList.current.set("3", dom)
|
||||||
|
}}
|
||||||
|
checkList={[
|
||||||
|
{
|
||||||
|
errMsg: "长度在 6-50 之间",
|
||||||
|
reg: /.{6,50}/
|
||||||
|
}
|
||||||
|
]}
|
||||||
required
|
required
|
||||||
fullWidth
|
fullWidth
|
||||||
label="密码"
|
label="密码"
|
||||||
@ -86,16 +118,6 @@ export default function SignUp() {
|
|||||||
autoComplete="new-password"
|
autoComplete="new-password"
|
||||||
/>
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={12}>
|
|
||||||
<TextField
|
|
||||||
required
|
|
||||||
fullWidth
|
|
||||||
label="确认密码"
|
|
||||||
type="password"
|
|
||||||
name="password1"
|
|
||||||
autoComplete="new-password"
|
|
||||||
/>
|
|
||||||
</Grid>
|
|
||||||
</Grid>
|
</Grid>
|
||||||
<Button
|
<Button
|
||||||
type="submit"
|
type="submit"
|
||||||
@ -114,6 +136,10 @@ export default function SignUp() {
|
|||||||
</Grid>
|
</Grid>
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
<Snackbar anchorOrigin={{ vertical: 'top', horizontal: 'center' }} open={regErr !== ""} onClose={() => setRegErr("")} >
|
||||||
|
<Alert onClose={() => setRegErr("")} severity="error">{regErr}</Alert>
|
||||||
|
</Snackbar>
|
||||||
|
{loading && <Loading />}
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user