import * as React from 'react'; import {useState} from 'react'; import TextField from "@mui/material/TextField"; import Button from "@mui/material/Button"; import Box from "@mui/material/Box"; import routes from "./routes"; import session from "../app/Session"; import Typography from "@mui/material/Typography"; import {NavLink} from "react-router-dom"; import AvatarBox from "./AvatarBox"; import {useTranslation} from "react-i18next"; import WarningAmberIcon from "@mui/icons-material/WarningAmber"; import accountApi, {AccountCreateLimitReachedError, UsernameTakenError} from "../app/AccountApi"; import {InputAdornment} from "@mui/material"; import IconButton from "@mui/material/IconButton"; import {Visibility, VisibilityOff} from "@mui/icons-material"; const Signup = () => { 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 { await accountApi.create(user.username, user.password); const token = await accountApi.login(user); console.log(`[Signup] User signup for user ${user.username} successful, token is ${token}`); session.store(user.username, token); window.location.href = routes.app; } catch (e) { console.log(`[Signup] Signup for user ${user.username} failed`, e); if ((e instanceof UsernameTakenError)) { setError(t("signup_error_username_taken", { username: e.username })); } else if ((e instanceof AccountCreateLimitReachedError)) { setError(t("signup_error_creation_limit_reached")); } else if (e.message) { setError(e.message); } else { setError(t("signup_error_unknown")) } } }; if (!config.enable_signup) { return ( {t("signup_disabled")} ); } return ( {t("signup_title")} setUsername(ev.target.value.trim())} autoFocus /> setPassword(ev.target.value.trim())} InputProps={{ endAdornment: ( setShowPassword(!showPassword)} onMouseDown={(ev) => ev.preventDefault()} edge="end" > {showPassword ? : } ) }} /> {error && {error} } {config.enable_login && {t("signup_already_have_account")} } ); } export default Signup;