2024-06-29 12:43:56 +00:00
|
|
|
import LockOutlinedIcon from "@mui/icons-material/LockOutlined";
|
|
|
|
import { Alert } from "@mui/material";
|
2024-06-29 11:26:12 +00:00
|
|
|
import Avatar from "@mui/material/Avatar";
|
2024-06-29 12:43:56 +00:00
|
|
|
import Box from "@mui/material/Box";
|
2024-06-29 11:26:12 +00:00
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
import CssBaseline from "@mui/material/CssBaseline";
|
2024-06-29 12:43:56 +00:00
|
|
|
import Grid from "@mui/material/Grid";
|
2024-06-29 11:26:12 +00:00
|
|
|
import Link from "@mui/material/Link";
|
|
|
|
import Paper from "@mui/material/Paper";
|
2024-06-29 12:43:56 +00:00
|
|
|
import TextField from "@mui/material/TextField";
|
2024-06-29 11:26:12 +00:00
|
|
|
import Typography from "@mui/material/Typography";
|
2024-06-29 12:43:56 +00:00
|
|
|
import * as React from "react";
|
|
|
|
import { useAlert } from "../hooks/context_providers/AlertDialogProvider";
|
|
|
|
import { useLoadingMessage } from "../hooks/context_providers/LoadingMessageProvider";
|
|
|
|
import { AuthApi } from "../api/AuthApi";
|
2024-06-29 11:26:12 +00:00
|
|
|
|
|
|
|
function Copyright(props: any) {
|
|
|
|
return (
|
|
|
|
<Typography
|
|
|
|
variant="body2"
|
|
|
|
color="text.secondary"
|
|
|
|
align="center"
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{"Copyright © "}
|
|
|
|
<Link color="inherit" href="https://0ph.fr/">
|
|
|
|
Pierre HUBERT
|
|
|
|
</Link>{" "}
|
|
|
|
{new Date().getFullYear()}
|
|
|
|
{"."}
|
|
|
|
</Typography>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function LoginRoute() {
|
2024-06-29 12:43:56 +00:00
|
|
|
const loadingMessage = useLoadingMessage();
|
|
|
|
|
2024-06-29 11:26:12 +00:00
|
|
|
const [user, setUser] = React.useState("");
|
|
|
|
const [password, setPassword] = React.useState("");
|
|
|
|
|
2024-06-29 12:43:56 +00:00
|
|
|
const [error, setError] = React.useState<string | undefined>();
|
|
|
|
|
|
|
|
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
|
2024-06-29 11:26:12 +00:00
|
|
|
event.preventDefault();
|
2024-06-29 12:43:56 +00:00
|
|
|
try {
|
|
|
|
loadingMessage.show("Signing in...");
|
|
|
|
setError(undefined);
|
|
|
|
|
|
|
|
await AuthApi.AuthWithPassword(user, password);
|
|
|
|
|
|
|
|
location.href = "/";
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to perform login!", e);
|
|
|
|
setError(`Failed to authenticate! ${e}`);
|
|
|
|
} finally {
|
|
|
|
loadingMessage.hide();
|
|
|
|
}
|
2024-06-29 11:26:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Grid container component="main" sx={{ height: "100vh" }}>
|
|
|
|
<CssBaseline />
|
|
|
|
<Grid
|
|
|
|
item
|
|
|
|
xs={false}
|
|
|
|
sm={4}
|
|
|
|
md={7}
|
|
|
|
sx={{
|
|
|
|
backgroundImage: 'url("/sun.jpg")',
|
|
|
|
backgroundColor: (t) =>
|
|
|
|
t.palette.mode === "light"
|
|
|
|
? t.palette.grey[50]
|
|
|
|
: t.palette.grey[900],
|
|
|
|
backgroundSize: "cover",
|
|
|
|
backgroundPosition: "left",
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<Grid item xs={12} sm={8} md={5} component={Paper} elevation={6} square>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
my: 8,
|
|
|
|
mx: 4,
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "center",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Avatar sx={{ m: 1, bgcolor: "secondary.main" }}>
|
|
|
|
<LockOutlinedIcon />
|
|
|
|
</Avatar>
|
|
|
|
<Typography component="h1" variant="h5">
|
|
|
|
SolarEnergy
|
|
|
|
</Typography>
|
2024-06-29 12:43:56 +00:00
|
|
|
|
|
|
|
{error && <Alert severity="error">{error}</Alert>}
|
|
|
|
|
2024-06-29 11:26:12 +00:00
|
|
|
<Box
|
|
|
|
component="form"
|
|
|
|
noValidate
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
sx={{ mt: 1 }}
|
|
|
|
>
|
|
|
|
<TextField
|
|
|
|
margin="normal"
|
|
|
|
required
|
|
|
|
fullWidth
|
|
|
|
label="Username"
|
|
|
|
autoFocus
|
|
|
|
value={user}
|
|
|
|
onChange={(v) => setUser(v.target.value)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<TextField
|
|
|
|
margin="normal"
|
|
|
|
required
|
|
|
|
fullWidth
|
|
|
|
label="Password"
|
|
|
|
type="password"
|
|
|
|
autoComplete="current-password"
|
|
|
|
value={password}
|
|
|
|
onChange={(v) => setPassword(v.target.value)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
type="submit"
|
|
|
|
fullWidth
|
|
|
|
variant="contained"
|
|
|
|
sx={{ mt: 3, mb: 2 }}
|
|
|
|
>
|
|
|
|
Sign In
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Copyright sx={{ mt: 5 }} />
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
|
|
|
}
|