import * as React from "react"; import { useState } from "react"; import { Typography, TextField, Button, Box, IconButton, InputAdornment } from "@mui/material"; import WarningAmberIcon from "@mui/icons-material/WarningAmber"; import { NavLink } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { Visibility, VisibilityOff } from "@mui/icons-material"; import accountApi from "../app/AccountApi"; import AvatarBox from "./AvatarBox"; import session from "../app/Session"; import routes from "./routes"; import { UnauthorizedError } from "../app/errors"; const Login = () => { const { t } = useTranslation(); const [error, setError] = useState(""); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const handleSubmit = async (event) => { event.preventDefault(); const user = { username, password }; try { const token = await accountApi.login(user); console.log(`[Login] User auth for user ${user.username} successful, token is ${token}`); session.store(user.username, token); window.location.href = routes.app; } catch (e) { console.log(`[Login] User auth for user ${user.username} failed`, e); if (e instanceof UnauthorizedError) { setError(t("Login failed: Invalid username or password")); } else { setError(e.message); } } }; if (!config.enable_login) { return ( {t("login_disabled")} ); } return ( {t("login_title")} setUsername(ev.target.value.trim())} autoFocus /> setPassword(ev.target.value.trim())} autoComplete="current-password" InputProps={{ endAdornment: ( setShowPassword(!showPassword)} onMouseDown={(ev) => ev.preventDefault()} edge="end" > {showPassword ? : } ), }} /> {error && ( {error} )} {/* This is where the password reset link would go */} {config.enable_signup && (
{t("login_link_signup")}
)}
); }; export default Login;