1
0
Fork 0
pingvin-share/frontend/src/components/auth/SignUpForm.tsx

111 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-04-25 15:15:17 +02:00
import {
Anchor,
Button,
Container,
Paper,
PasswordInput,
Text,
TextInput,
Title,
} from "@mantine/core";
import { useForm, yupResolver } from "@mantine/form";
2022-10-31 11:20:54 +01:00
import Link from "next/link";
import { useRouter } from "next/router";
import { FormattedMessage } from "react-intl";
2022-04-25 15:15:17 +02:00
import * as yup from "yup";
import useConfig from "../../hooks/config.hook";
import useTranslate from "../../hooks/useTranslate.hook";
import useUser from "../../hooks/user.hook";
import authService from "../../services/auth.service";
2022-04-25 15:15:17 +02:00
import toast from "../../utils/toast.util";
2024-03-22 12:38:58 +01:00
const webroot = process.env.WEBROOT || "";
2022-12-01 23:07:49 +01:00
const SignUpForm = () => {
const config = useConfig();
const router = useRouter();
const t = useTranslate();
const { refreshUser } = useUser();
2022-04-25 15:15:17 +02:00
const validationSchema = yup.object().shape({
email: yup.string().email(t("common.error.invalid-email")).required(),
username: yup
.string()
.min(3, t("common.error.too-short", { length: 3 }))
.required(t("common.error.field-required")),
password: yup
.string()
.min(8, t("common.error.too-short", { length: 8 }))
.required(t("common.error.field-required")),
2022-04-25 15:15:17 +02:00
});
const form = useForm({
initialValues: {
email: "",
2022-12-01 23:07:49 +01:00
username: "",
2022-04-25 15:15:17 +02:00
password: "",
},
2022-10-10 22:14:23 +02:00
validate: yupResolver(validationSchema),
2022-04-25 15:15:17 +02:00
});
const signUp = async (email: string, username: string, password: string) => {
await authService
2022-12-01 23:07:49 +01:00
.signUp(email, username, password)
.then(async () => {
const user = await refreshUser();
if (user?.isAdmin) {
2024-03-22 12:38:58 +01:00
router.replace(webroot + "/admin/intro");
} else {
2024-03-22 12:38:58 +01:00
router.replace(webroot + "/upload");
}
})
2022-12-05 15:53:24 +01:00
.catch(toast.axiosError);
2022-04-25 15:15:17 +02:00
};
2022-04-25 15:15:17 +02:00
return (
<Container size={420} my={40}>
2023-02-09 18:17:53 +01:00
<Title order={2} align="center" weight={900}>
<FormattedMessage id="signup.title" />
2022-04-25 15:15:17 +02:00
</Title>
{config.get("share.allowRegistration") && (
2022-05-02 11:19:24 +02:00
<Text color="dimmed" size="sm" align="center" mt={5}>
<FormattedMessage id="signup.description" />{" "}
2022-12-01 23:07:49 +01:00
<Anchor component={Link} href={"signIn"} size="sm">
<FormattedMessage id="signup.button.signin" />
2022-05-02 11:19:24 +02:00
</Anchor>
</Text>
)}
2022-04-25 15:15:17 +02:00
<Paper withBorder shadow="md" p={30} mt={30} radius="md">
<form
onSubmit={form.onSubmit((values) =>
2023-08-17 14:47:58 +02:00
signUp(values.email, values.username, values.password),
2022-04-25 15:15:17 +02:00
)}
>
2022-12-01 23:07:49 +01:00
<TextInput
label={t("signup.input.username")}
placeholder={t("signup.input.username.placeholder")}
2022-12-01 23:07:49 +01:00
{...form.getInputProps("username")}
/>
2022-04-25 15:15:17 +02:00
<TextInput
label={t("signup.input.email")}
placeholder={t("signup.input.email.placeholder")}
2022-12-01 23:07:49 +01:00
mt="md"
2022-04-25 15:15:17 +02:00
{...form.getInputProps("email")}
/>
<PasswordInput
label={t("signin.input.password")}
placeholder={t("signin.input.password.placeholder")}
2022-04-25 15:15:17 +02:00
mt="md"
{...form.getInputProps("password")}
/>
<Button fullWidth mt="xl" type="submit">
<FormattedMessage id="signup.button.submit" />
2022-04-25 15:15:17 +02:00
</Button>
</form>
</Paper>
</Container>
);
};
2022-12-01 23:07:49 +01:00
export default SignUpForm;