ntfy/web/src/components/Signup.js

125 lines
4.5 KiB
JavaScript
Raw Normal View History

2022-12-14 11:36:53 +01:00
import * as React from 'react';
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";
import Box from "@mui/material/Box";
import api from "../app/Api";
import routes from "./routes";
import session from "../app/Session";
2022-12-15 05:11:22 +01:00
import Typography from "@mui/material/Typography";
import {NavLink} from "react-router-dom";
2022-12-21 19:19:07 +01:00
import AvatarBox from "./AvatarBox";
import {useTranslation} from "react-i18next";
2022-12-22 03:55:39 +01:00
import {useState} from "react";
import WarningAmberIcon from "@mui/icons-material/WarningAmber";
2022-12-14 11:36:53 +01:00
const Signup = () => {
2022-12-21 19:19:07 +01:00
const { t } = useTranslation();
2022-12-22 03:55:39 +01:00
const [error, setError] = useState("");
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [confirm, setConfirm] = useState("");
2022-12-14 11:36:53 +01:00
const handleSubmit = async (event) => {
event.preventDefault();
2022-12-22 03:55:39 +01:00
const user = { username, password };
try {
await api.createAccount(config.baseUrl, user.username, user.password);
const token = await api.login(config.baseUrl, user);
if (token) {
console.log(`[Signup] User signup for user ${user.username} successful, token is ${token}`);
session.store(user.username, token);
window.location.href = routes.app;
} else {
console.log(`[Signup] Signup for user ${user.username} failed, access denied`);
setError(t("Login failed: Invalid username or password"));
}
} catch (e) {
console.log(`[Signup] Signup for user ${user.username} failed`, e);
if (e && e.message) {
setError(e.message);
} else {
setError(t("Unknown error. Check logs for details."))
}
}
2022-12-14 11:36:53 +01:00
};
2022-12-21 19:19:07 +01:00
if (!config.enableSignup) {
return (
<AvatarBox>
<Typography sx={{ typography: 'h6' }}>{t("Signup is disabled")}</Typography>
</AvatarBox>
);
}
2022-12-14 11:36:53 +01:00
return (
2022-12-21 19:19:07 +01:00
<AvatarBox>
2022-12-15 05:11:22 +01:00
<Typography sx={{ typography: 'h6' }}>
2022-12-21 19:19:07 +01:00
{t("Create a ntfy account")}
2022-12-15 05:11:22 +01:00
</Typography>
<Box component="form" onSubmit={handleSubmit} noValidate sx={{mt: 1, maxWidth: 400}}>
<TextField
margin="dense"
required
fullWidth
id="username"
label="Username"
name="username"
2022-12-22 03:55:39 +01:00
value={username}
onChange={ev => setUsername(ev.target.value.trim())}
2022-12-15 05:11:22 +01:00
autoFocus
/>
<TextField
margin="dense"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
2022-12-22 03:55:39 +01:00
value={password}
onChange={ev => setPassword(ev.target.value.trim())}
2022-12-15 05:11:22 +01:00
/>
<TextField
margin="dense"
required
fullWidth
name="confirm-password"
label="Confirm password"
type="password"
id="confirm-password"
2022-12-22 03:55:39 +01:00
value={confirm}
onChange={ev => setConfirm(ev.target.value.trim())}
2022-12-15 05:11:22 +01:00
/>
<Button
type="submit"
fullWidth
variant="contained"
2022-12-22 03:55:39 +01:00
disabled={username === "" || password === "" || password !== confirm}
2022-12-15 05:11:22 +01:00
sx={{mt: 2, mb: 2}}
>
2022-12-21 19:19:07 +01:00
{t("Sign up")}
2022-12-15 05:11:22 +01:00
</Button>
2022-12-22 03:55:39 +01:00
{error &&
<Box sx={{
mb: 1,
display: 'flex',
flexGrow: 1,
justifyContent: 'center',
}}>
<WarningAmberIcon color="error" sx={{mr: 1}}/>
<Typography sx={{color: 'error.main'}}>{error}</Typography>
</Box>
}
2022-12-14 11:36:53 +01:00
</Box>
2022-12-21 19:19:07 +01:00
{config.enableLogin &&
<Typography sx={{mb: 4}}>
<NavLink to={routes.login} variant="body1">
{t("Already have an account? Sign in!")}
</NavLink>
</Typography>
}
</AvatarBox>
2022-12-14 11:36:53 +01:00
);
}
export default Signup;