import VisibilityIcon from '@mui/icons-material/Visibility'; import VisibilityOffIcon from '@mui/icons-material/VisibilityOff'; import { Alert, CircularProgress, FormControl, IconButton, InputAdornment, InputLabel, OutlinedInput, Tooltip, } from "@mui/material"; import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; import TextField from "@mui/material/TextField"; import Typography from "@mui/material/Typography"; import * as React from "react"; import { useNavigate } from "react-router-dom"; import { useAuth } from "../../App"; import { AuthApi } from "../../api/AuthApi"; import { ServerApi } from "../../api/ServerApi"; /** * Login form */ export function LoginRoute(): React.ReactElement { const [loading, setLoading] = React.useState(false); const [error, setError] = React.useState(null); const auth = useAuth(); const navigate = useNavigate(); const [username, setUsername] = React.useState(""); const [password, setPassword] = React.useState(""); const canSubmit = username.length > 0 && password.length > 0; const [showPassword, setShowPassword] = React.useState(false); const handleClickShowPassword = () => { setShowPassword((show) => !show); }; const handleMouseDownPassword = ( event: React.MouseEvent ) => { event.preventDefault(); }; const handleLoginSubmit = async (event: React.FormEvent) => { event.preventDefault(); if (!canSubmit) return; try { await AuthApi.LoginWithPassword(username, password); navigate("/"); auth.setSignedIn(true); } catch (e) { console.error(e); setError("Auth failed!"); } setLoading(false); }; const authWithOpenID = async () => { try { setLoading(true); const res = await AuthApi.StartOpenIDLogin(); window.location.href = res.url; } catch (e) { console.error(e); setError("Failed to initialize OpenID login"); } }; if (loading) return ( <> ); return ( <> {error && ( {error} )} {ServerApi.Config.local_auth_enabled && ( <> Local authentication { setUsername(e.target.value); }} autoComplete="username" autoFocus /> Password { setPassword(e.target.value); }} autoComplete="current-password" endAdornment={ {showPassword ? : } } /> )}
{ServerApi.Config.oidc_auth_enabled && ( )}
); }