2022-12-03 09:37:48 +13:00
|
|
|
import * as React from "react";
|
2022-12-26 05:59:44 +13:00
|
|
|
import { useState } from "react";
|
2023-05-24 22:25:20 +12:00
|
|
|
import { Typography, TextField, Button, Box, IconButton, InputAdornment } from "@mui/material";
|
2022-12-22 07:19:07 +13:00
|
|
|
import WarningAmberIcon from "@mui/icons-material/WarningAmber";
|
2022-12-15 17:11:22 +13:00
|
|
|
import { NavLink } from "react-router-dom";
|
2022-12-22 07:19:07 +13:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-12-31 08:20:48 +13:00
|
|
|
import { Visibility, VisibilityOff } from "@mui/icons-material";
|
2023-02-03 09:19:37 +13:00
|
|
|
import accountApi from "../app/AccountApi";
|
2022-12-22 07:19:07 +13:00
|
|
|
import AvatarBox from "./AvatarBox";
|
2022-12-03 09:37:48 +13:00
|
|
|
import session from "../app/Session";
|
|
|
|
import routes from "./routes";
|
2023-02-03 09:19:37 +13:00
|
|
|
import { UnauthorizedError } from "../app/errors";
|
2022-12-03 09:37:48 +13:00
|
|
|
|
|
|
|
const Login = () => {
|
2022-12-22 07:19:07 +13:00
|
|
|
const { t } = useTranslation();
|
|
|
|
const [error, setError] = useState("");
|
2022-12-22 15:55:39 +13:00
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
2022-12-31 08:20:48 +13:00
|
|
|
const [showPassword, setShowPassword] = useState(false);
|
|
|
|
|
2022-12-03 09:37:48 +13:00
|
|
|
const handleSubmit = async (event) => {
|
|
|
|
event.preventDefault();
|
2022-12-22 15:55:39 +13:00
|
|
|
const user = { username, password };
|
2022-12-22 07:19:07 +13:00
|
|
|
try {
|
2022-12-26 05:59:44 +13:00
|
|
|
const token = await accountApi.login(user);
|
2022-12-25 09:51:22 +13:00
|
|
|
console.log(`[Login] User auth for user ${user.username} successful, token is ${token}`);
|
2023-06-14 00:00:51 +12:00
|
|
|
await session.store(user.username, token);
|
2022-12-25 09:51:22 +13:00
|
|
|
window.location.href = routes.app;
|
2022-12-22 07:19:07 +13:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`[Login] User auth for user ${user.username} failed`, e);
|
2023-02-03 09:19:37 +13:00
|
|
|
if (e instanceof UnauthorizedError) {
|
2022-12-25 09:51:22 +13:00
|
|
|
setError(t("Login failed: Invalid username or password"));
|
2022-12-22 07:19:07 +13:00
|
|
|
} else {
|
2023-02-03 09:19:37 +13:00
|
|
|
setError(e.message);
|
2022-12-22 07:19:07 +13:00
|
|
|
}
|
|
|
|
}
|
2023-05-24 07:13:01 +12:00
|
|
|
};
|
2023-01-05 16:47:12 +13:00
|
|
|
if (!config.enable_login) {
|
2022-12-03 09:37:48 +13:00
|
|
|
return (
|
2022-12-15 17:11:22 +13:00
|
|
|
<AvatarBox>
|
2022-12-31 08:20:48 +13:00
|
|
|
<Typography sx={{ typography: "h6" }}>{t("login_disabled")}</Typography>
|
2023-01-05 16:47:12 +13:00
|
|
|
</AvatarBox>
|
2022-12-03 09:37:48 +13:00
|
|
|
);
|
|
|
|
}
|
2023-05-24 07:13:01 +12:00
|
|
|
return (
|
2022-12-22 07:19:07 +13:00
|
|
|
<AvatarBox>
|
2023-02-12 04:49:37 +13:00
|
|
|
<Typography sx={{ typography: "h6" }}>{t("login_title")}</Typography>
|
2022-12-15 17:11:22 +13:00
|
|
|
<Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1, maxWidth: 400 }}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2023-05-24 07:13:01 +12:00
|
|
|
required
|
2022-12-15 17:11:22 +13:00
|
|
|
fullWidth
|
|
|
|
id="username"
|
2022-12-30 02:20:53 +13:00
|
|
|
label={t("signup_form_username")}
|
2022-12-15 17:11:22 +13:00
|
|
|
name="username"
|
2022-12-22 15:55:39 +13:00
|
|
|
value={username}
|
|
|
|
onChange={(ev) => setUsername(ev.target.value.trim())}
|
2022-12-15 17:11:22 +13:00
|
|
|
autoFocus
|
2023-05-24 07:13:01 +12:00
|
|
|
/>
|
2022-12-15 17:11:22 +13:00
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2023-05-24 07:13:01 +12:00
|
|
|
required
|
2022-12-15 17:11:22 +13:00
|
|
|
fullWidth
|
|
|
|
name="password"
|
2022-12-30 02:20:53 +13:00
|
|
|
label={t("signup_form_password")}
|
2022-12-31 08:20:48 +13:00
|
|
|
type={showPassword ? "text" : "password"}
|
2022-12-15 17:11:22 +13:00
|
|
|
id="password"
|
2022-12-22 15:55:39 +13:00
|
|
|
value={password}
|
|
|
|
onChange={(ev) => setPassword(ev.target.value.trim())}
|
2022-12-15 17:11:22 +13:00
|
|
|
autoComplete="current-password"
|
2022-12-31 08:20:48 +13:00
|
|
|
InputProps={{
|
|
|
|
endAdornment: (
|
|
|
|
<InputAdornment position="end">
|
|
|
|
<IconButton
|
|
|
|
aria-label={t("signup_form_toggle_password_visibility")}
|
|
|
|
onClick={() => setShowPassword(!showPassword)}
|
|
|
|
onMouseDown={(ev) => ev.preventDefault()}
|
|
|
|
edge="end"
|
2023-05-24 07:13:01 +12:00
|
|
|
>
|
2022-12-31 08:20:48 +13:00
|
|
|
{showPassword ? <VisibilityOff /> : <Visibility />}
|
|
|
|
</IconButton>
|
|
|
|
</InputAdornment>
|
2023-05-24 07:13:01 +12:00
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
2022-12-22 15:55:39 +13:00
|
|
|
<Button type="submit" fullWidth variant="contained" disabled={username === "" || password === ""} sx={{ mt: 2, mb: 2 }}>
|
2022-12-30 02:20:53 +13:00
|
|
|
{t("login_form_button_submit")}
|
2022-12-15 17:11:22 +13:00
|
|
|
</Button>
|
2022-12-22 07:19:07 +13:00
|
|
|
{error && (
|
2023-05-24 07:13:01 +12:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
mb: 1,
|
2022-12-22 07:19:07 +13:00
|
|
|
display: "flex",
|
|
|
|
flexGrow: 1,
|
|
|
|
justifyContent: "center",
|
2023-05-24 07:13:01 +12:00
|
|
|
}}
|
|
|
|
>
|
2022-12-22 07:19:07 +13:00
|
|
|
<WarningAmberIcon color="error" sx={{ mr: 1 }} />
|
|
|
|
<Typography sx={{ color: "error.main" }}>{error}</Typography>
|
2023-05-24 07:13:01 +12:00
|
|
|
</Box>
|
|
|
|
)}
|
2022-12-15 17:11:22 +13:00
|
|
|
<Box sx={{ width: "100%" }}>
|
2023-01-11 16:51:51 +13:00
|
|
|
{/* This is where the password reset link would go */}
|
2023-01-05 16:47:12 +13:00
|
|
|
{config.enable_signup && (
|
|
|
|
<div style={{ float: "right" }}>
|
|
|
|
<NavLink to={routes.signup} variant="body1">
|
|
|
|
{t("login_link_signup")}
|
2023-05-24 07:13:01 +12:00
|
|
|
</NavLink>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</Box>
|
2022-12-22 07:19:07 +13:00
|
|
|
</AvatarBox>
|
2023-05-24 07:13:01 +12:00
|
|
|
);
|
|
|
|
};
|
2022-12-03 09:37:48 +13:00
|
|
|
|
|
|
|
export default Login;
|