domain-watchdog/assets/pages/LoginPage.tsx

110 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-07-23 18:37:59 +02:00
import * as React from 'react';
2024-07-23 20:34:27 +02:00
import {useState} from 'react';
2024-07-23 18:37:59 +02:00
import Avatar from '@mui/material/Avatar';
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Box from '@mui/material/Box';
import LockOutlinedIcon from '@mui/icons-material/LockOutlined';
import Typography from '@mui/material/Typography';
import Link from "@mui/material/Link";
import {login} from "../utils/api";
import {useNavigate} from "react-router-dom";
2024-07-23 20:34:27 +02:00
import {Alert} from "@mui/material";
2024-07-23 21:57:23 +02:00
import Container from "@mui/material/Container";
2024-07-25 02:09:49 +02:00
import Footer from "../components/Footer";
2024-07-23 18:37:59 +02:00
interface Props {
setIsAuthenticated: (val: boolean) => void
}
2024-07-23 20:34:27 +02:00
export default function LoginPage({setIsAuthenticated}: Props) {
const navigate = useNavigate()
2024-07-23 18:37:59 +02:00
2024-07-23 20:34:27 +02:00
const [error, setError] = useState<string>('')
const [credentials, setCredentials] = useState({email: "", password: ""})
2024-07-23 18:37:59 +02:00
2024-07-23 20:34:27 +02:00
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
2024-07-23 18:37:59 +02:00
try {
2024-07-23 20:34:27 +02:00
await login(credentials.email, credentials.password);
2024-07-23 18:37:59 +02:00
setIsAuthenticated(true)
2024-07-23 20:34:27 +02:00
navigate('/');
2024-07-23 18:37:59 +02:00
2024-07-23 20:34:27 +02:00
} catch (e: any) {
2024-07-25 02:09:49 +02:00
setCredentials({...credentials, password: ""})
2024-07-23 20:34:27 +02:00
setError(e.response.data.message)
2024-07-23 18:37:59 +02:00
}
};
return (
<>
<Container component="main" maxWidth="xs">
<Box
sx={{
marginTop: 20,
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
}}
>
<Avatar sx={{m: 1, bgcolor: 'secondary.main'}}>
<LockOutlinedIcon/>
</Avatar>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<Box component="form" onSubmit={handleSubmit} noValidate sx={{mt: 1}}>
2024-07-23 20:34:27 +02:00
{
error !== "" && <Alert variant="outlined" severity="error">{error}</Alert>
}
2024-07-23 18:37:59 +02:00
<TextField
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
2024-07-23 20:34:27 +02:00
value={credentials.email}
onChange={(e) => setCredentials({...credentials, email: e.currentTarget.value})}
2024-07-23 18:37:59 +02:00
/>
<TextField
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
2024-07-23 20:34:27 +02:00
value={credentials.password}
2024-07-23 18:37:59 +02:00
autoComplete="current-password"
2024-07-23 20:34:27 +02:00
onChange={(e) => setCredentials({...credentials, password: e.currentTarget.value})}
2024-07-23 18:37:59 +02:00
/>
<Button
type="submit"
fullWidth
variant="contained"
sx={{mt: 3}}
>
Sign In
</Button>
</Box>
</Box>
<Link href="/login/oauth">
<Button
type="button"
fullWidth
color='secondary'
variant="contained"
sx={{mt: 3, mb: 2}}
>
Single Sign-On
</Button>
</Link>
2024-07-25 02:09:49 +02:00
<Footer/>
2024-07-23 18:37:59 +02:00
</Container>
</>
);
}