lsp-yggdrasil-web/src/pages/login.tsx

82 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-07-06 16:06:18 +00:00
import { Box, Button, CssBaseline, Grid, Link, Paper, TextField, ThemeProvider, Typography, Alert, Collapse } from "@mui/material"
import { useNavigate } from "react-router-dom"
import { api, theme } from '../index'
import axios from "axios"
import { useRef, useState } from "react"
2022-07-06 19:15:23 +00:00
import "./custom.css"
2022-07-06 16:06:18 +00:00
export const PageLogin = () => {
const navigate = useNavigate()
const usernameInput = useRef<HTMLInputElement>(null)
const passwordInput = useRef<HTMLInputElement>(null)
const [illegal, setIllegal] = useState(false)
const handleLogin = () => {
const username = usernameInput.current?.value
const password = passwordInput.current?.value
if(!username || !password) {
return setIllegal(true)
}
axios.post(api("/api/login"), {
username,
password,
createToken: false
}).then((response) => {
window.sessionStorage.setItem('identifier', response.data.identifier)
window.sessionStorage.setItem('textures', response.data.texturs)
window.sessionStorage.setItem('username', response.data.username)
window.sessionStorage.setItem('uuid', response.data.uuid)
})
}
return <ThemeProvider theme={theme}>
<CssBaseline />
2022-07-06 19:15:23 +00:00
<div className="rootPanel">
2022-07-06 16:06:18 +00:00
2022-07-06 19:15:23 +00:00
<Box>
2022-07-06 16:06:18 +00:00
<Grid container direction="column" justifyContent="center" alignItems="center" spacing={1}>
2022-07-06 19:15:23 +00:00
<Paper elevation={3} className="inlineBox">
2022-07-06 16:06:18 +00:00
<Grid container direction="column" justifyContent="center" alignItems="center" spacing={2}>
2022-07-06 19:15:23 +00:00
<Grid item className="title">
2022-07-06 16:06:18 +00:00
<Typography variant="h5">
</Typography>
</Grid>
2022-07-06 19:15:23 +00:00
<Grid item>
2022-07-06 16:06:18 +00:00
<Collapse in={illegal}>
<Alert severity="error">
2022-07-06 19:15:23 +00:00
2022-07-06 16:06:18 +00:00
</Alert>
</Collapse>
</Grid>
2022-07-06 19:15:23 +00:00
<Grid item>
2022-07-06 16:06:18 +00:00
<TextField fullWidth required label="用户名" type="text" autoComplete="current-username" inputRef={usernameInput} onInput={() => setIllegal(false)} />
</Grid>
2022-07-06 19:15:23 +00:00
<Grid item>
2022-07-06 16:06:18 +00:00
<TextField fullWidth required label="密码" type="password" autoComplete="current-password" inputRef={passwordInput} onInput={() => setIllegal(false)} />
</Grid>
2022-07-06 19:15:23 +00:00
<Grid item>
<Button onClick={handleLogin} variant="contained" color="primary" >
2022-07-06 16:06:18 +00:00
</Button>
</Grid>
2022-07-06 19:15:23 +00:00
<Grid item>
2022-07-06 16:06:18 +00:00
<Link onClick={() => { navigate("/register") }} underline="always"> ? </Link>
</Grid>
</Grid>
</Paper>
</Grid>
</Box>
</div>
</ThemeProvider>
}