Debian Codegraph @

web/src/components/Login.jsx

raw
1 import * as React from "react";
2 import { useState } from "react";
3 import { Typography, TextField, Button, Box, IconButton, InputAdornment } from "@mui/material";
4 import WarningAmberIcon from "@mui/icons-material/WarningAmber";
5 import { NavLink } from "react-router-dom";
6 import { useTranslation } from "react-i18next";
7 import Visibility from "@mui/icons-material/Visibility";
8 import VisibilityOff from "@mui/icons-material/VisibilityOff";
9 import accountApi from "../app/AccountApi";
10 import AvatarBox from "./AvatarBox";
11 import session from "../app/Session";
12 import routes from "./routes";
13 import { UnauthorizedError } from "../app/errors";
14 import { fadeReload } from "../app/transition";
15
16 const Login = () => {
17 const { t } = useTranslation();
18 const [error, setError] = useState("");
19 const [username, setUsername] = useState("");
20 const [password, setPassword] = useState("");
21 const [showPassword, setShowPassword] = useState(false);
22
23 const handleSubmit = async (event) => {
24 event.preventDefault();
25 const user = { username, password };
26 try {
27 const { token, username: canonicalUsername } = await accountApi.login(user);
28 console.log(`[Login] User auth for user ${user.username} successful, logged in as ${canonicalUsername}`);
29 await session.store(canonicalUsername, token);
30 fadeReload(routes.app);
31 } catch (e) {
32 console.log(`[Login] User auth for user ${user.username} failed`, e);
33 if (e instanceof UnauthorizedError) {
34 setError(t("Login failed: Invalid username/email or password"));
35 } else {
36 setError(e.message);
37 }
38 }
39 };
40 if (!config.enable_login) {
41 return (
42 <AvatarBox>
43 <Typography sx={{ typography: "h6" }}>{t("login_disabled")}</Typography>
44 </AvatarBox>
45 );
46 }
47 return (
48 <AvatarBox>
49 <Typography sx={{ typography: "h6" }}>{t("login_title")}</Typography>
50 <Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}>
51 <TextField
52 margin="dense"
53 required
54 fullWidth
55 id="username"
56 label={t("login_form_username_label")}
57 name="username"
58 value={username}
59 onChange={(ev) => setUsername(ev.target.value.trim())}
60 autoFocus
61 />
62 <TextField
63 margin="dense"
64 required
65 fullWidth
66 name="password"
67 label={t("signup_form_password")}
68 type={showPassword ? "text" : "password"}
69 id="password"
70 value={password}
71 onChange={(ev) => setPassword(ev.target.value)}
72 autoComplete="current-password"
73 slotProps={{
74 input: {
75 endAdornment: (
76 <InputAdornment position="end">
77 <IconButton
78 aria-label={t("signup_form_toggle_password_visibility")}
79 onClick={() => setShowPassword(!showPassword)}
80 onMouseDown={(ev) => ev.preventDefault()}
81 edge="end"
82 >
83 {showPassword ? <VisibilityOff /> : <Visibility />}
84 </IconButton>
85 </InputAdornment>
86 ),
87 },
88 }}
89 />
90 <Button type="submit" fullWidth variant="contained" disabled={username === "" || password === ""} sx={{ mt: 2, mb: 2 }}>
91 {t("login_form_button_submit")}
92 </Button>
93 {error && (
94 <Box
95 sx={{
96 mb: 1,
97 display: "flex",
98 flexGrow: 1,
99 justifyContent: "center",
100 }}
101 >
102 <WarningAmberIcon color="error" sx={{ mr: 1 }} />
103 <Typography sx={{ color: "error.main" }}>{error}</Typography>
104 </Box>
105 )}
106 <Box sx={{ width: "100%" }}>
107 {config.enable_reset_password && (
108 <div style={{ float: "left" }}>
109 <NavLink to={routes.passwordResetRequest} variant="body1">
110 {t("login_link_forgot_password")}
111 </NavLink>
112 </div>
113 )}
114 {config.enable_signup && (
115 <div style={{ float: "right" }}>
116 <NavLink to={routes.signup} variant="body1">
117 {t("login_link_signup")}
118 </NavLink>
119 </div>
120 )}
121 </Box>
122 </Box>
123 </AvatarBox>
124 );
125 };
126
127 export default Login;