| 1 |
import * as React from "react"; |
| 2 |
import { useState } from "react"; |
| 3 |
import { TextField, Button, Box, Typography, InputAdornment, IconButton } from "@mui/material"; |
| 4 |
import { NavLink } from "react-router-dom"; |
| 5 |
import { useTranslation } from "react-i18next"; |
| 6 |
import WarningAmberIcon from "@mui/icons-material/WarningAmber"; |
| 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 { AccountActionLimitReachedError, UserExistsError } from "../app/errors"; |
| 14 |
import { fadeReload } from "../app/transition"; |
| 15 |
|
| 16 |
const Signup = () => { |
| 17 |
const { t } = useTranslation(); |
| 18 |
const [error, setError] = useState(""); |
| 19 |
const [username, setUsername] = useState(""); |
| 20 |
const [email, setEmail] = useState(""); |
| 21 |
const [password, setPassword] = useState(""); |
| 22 |
const [confirm, setConfirm] = useState(""); |
| 23 |
const [showPassword, setShowPassword] = useState(false); |
| 24 |
const [showConfirm, setShowConfirm] = useState(false); |
| 25 |
|
| 26 |
const handleSubmit = async (event) => { |
| 27 |
event.preventDefault(); |
| 28 |
const user = { username, password }; |
| 29 |
try { |
| 30 |
await accountApi.create(user.username, user.password, email); |
| 31 |
const { token, username: canonicalUsername } = await accountApi.login(user); |
| 32 |
console.log(`[Signup] User signup for user ${user.username} successful, logged in as ${canonicalUsername}`); |
| 33 |
await session.store(canonicalUsername, token); |
| 34 |
fadeReload(routes.app); |
| 35 |
} catch (e) { |
| 36 |
console.log(`[Signup] Signup for user ${user.username} failed`, e); |
| 37 |
if (e instanceof UserExistsError) { |
| 38 |
setError(t("signup_error_username_taken", { username: e.username })); |
| 39 |
} else if (e instanceof AccountActionLimitReachedError) { |
| 40 |
setError(t("signup_error_creation_limit_reached")); |
| 41 |
} else { |
| 42 |
setError(e.message); |
| 43 |
} |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
if (!config.enable_signup) { |
| 48 |
return ( |
| 49 |
<AvatarBox> |
| 50 |
<Typography sx={{ typography: "h6" }}>{t("signup_disabled")}</Typography> |
| 51 |
</AvatarBox> |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
return ( |
| 56 |
<AvatarBox> |
| 57 |
<Typography sx={{ typography: "h6" }}>{t("signup_title")}</Typography> |
| 58 |
<Box component="form" onSubmit={handleSubmit} noValidate sx={{ mt: 1 }}> |
| 59 |
<TextField |
| 60 |
margin="dense" |
| 61 |
required |
| 62 |
fullWidth |
| 63 |
id="username" |
| 64 |
label={t("signup_form_username")} |
| 65 |
name="username" |
| 66 |
value={username} |
| 67 |
onChange={(ev) => setUsername(ev.target.value.trim())} |
| 68 |
autoFocus |
| 69 |
/> |
| 70 |
{config.enable_emails && ( |
| 71 |
<TextField |
| 72 |
margin="dense" |
| 73 |
fullWidth |
| 74 |
id="email" |
| 75 |
label={t("signup_form_email")} |
| 76 |
name="email" |
| 77 |
type="email" |
| 78 |
value={email} |
| 79 |
onChange={(ev) => setEmail(ev.target.value.trim())} |
| 80 |
/> |
| 81 |
)} |
| 82 |
<TextField |
| 83 |
margin="dense" |
| 84 |
required |
| 85 |
fullWidth |
| 86 |
name="password" |
| 87 |
label={t("signup_form_password")} |
| 88 |
type={showPassword ? "text" : "password"} |
| 89 |
id="password" |
| 90 |
autoComplete="new-password" |
| 91 |
value={password} |
| 92 |
onChange={(ev) => setPassword(ev.target.value)} |
| 93 |
slotProps={{ |
| 94 |
input: { |
| 95 |
endAdornment: ( |
| 96 |
<InputAdornment position="end"> |
| 97 |
<IconButton |
| 98 |
aria-label={t("signup_form_toggle_password_visibility")} |
| 99 |
onClick={() => setShowPassword(!showPassword)} |
| 100 |
onMouseDown={(ev) => ev.preventDefault()} |
| 101 |
edge="end" |
| 102 |
> |
| 103 |
{showPassword ? <VisibilityOff /> : <Visibility />} |
| 104 |
</IconButton> |
| 105 |
</InputAdornment> |
| 106 |
), |
| 107 |
}, |
| 108 |
}} |
| 109 |
/> |
| 110 |
<TextField |
| 111 |
margin="dense" |
| 112 |
required |
| 113 |
fullWidth |
| 114 |
name="password" |
| 115 |
label={t("signup_form_confirm_password")} |
| 116 |
type={showConfirm ? "text" : "password"} |
| 117 |
id="confirm" |
| 118 |
autoComplete="new-password" |
| 119 |
value={confirm} |
| 120 |
onChange={(ev) => setConfirm(ev.target.value)} |
| 121 |
slotProps={{ |
| 122 |
input: { |
| 123 |
endAdornment: ( |
| 124 |
<InputAdornment position="end"> |
| 125 |
<IconButton |
| 126 |
aria-label={t("signup_form_toggle_password_visibility")} |
| 127 |
onClick={() => setShowConfirm(!showConfirm)} |
| 128 |
onMouseDown={(ev) => ev.preventDefault()} |
| 129 |
edge="end" |
| 130 |
> |
| 131 |
{showConfirm ? <VisibilityOff /> : <Visibility />} |
| 132 |
</IconButton> |
| 133 |
</InputAdornment> |
| 134 |
), |
| 135 |
}, |
| 136 |
}} |
| 137 |
/> |
| 138 |
<Button |
| 139 |
type="submit" |
| 140 |
fullWidth |
| 141 |
variant="contained" |
| 142 |
disabled={username === "" || password === "" || password !== confirm} |
| 143 |
sx={{ mt: 2, mb: 2 }} |
| 144 |
> |
| 145 |
{t("signup_form_button_submit")} |
| 146 |
</Button> |
| 147 |
{error && ( |
| 148 |
<Box |
| 149 |
sx={{ |
| 150 |
mb: 1, |
| 151 |
display: "flex", |
| 152 |
flexGrow: 1, |
| 153 |
justifyContent: "center", |
| 154 |
}} |
| 155 |
> |
| 156 |
<WarningAmberIcon color="error" sx={{ mr: 1 }} /> |
| 157 |
<Typography sx={{ color: "error.main" }}>{error}</Typography> |
| 158 |
</Box> |
| 159 |
)} |
| 160 |
</Box> |
| 161 |
{config.enable_login && ( |
| 162 |
<Typography sx={{ mb: 4 }}> |
| 163 |
<NavLink to={routes.login} variant="body1"> |
| 164 |
{t("signup_already_have_account")} |
| 165 |
</NavLink> |
| 166 |
</Typography> |
| 167 |
)} |
| 168 |
</AvatarBox> |
| 169 |
); |
| 170 |
}; |
| 171 |
|
| 172 |
export default Signup; |