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-09 22:30:32 +02:00
|
|
|
import getConfig from "next/config";
|
2022-04-25 15:15:17 +02:00
|
|
|
import * as yup from "yup";
|
2022-10-09 22:30:32 +02:00
|
|
|
import authService from "../../services/auth.service";
|
2022-04-25 15:15:17 +02:00
|
|
|
import toast from "../../utils/toast.util";
|
|
|
|
|
|
2022-10-09 22:30:32 +02:00
|
|
|
const { publicRuntimeConfig } = getConfig();
|
|
|
|
|
|
2022-04-25 15:15:17 +02:00
|
|
|
const AuthForm = ({ mode }: { mode: "signUp" | "signIn" }) => {
|
|
|
|
|
const validationSchema = yup.object().shape({
|
|
|
|
|
email: yup.string().email().required(),
|
|
|
|
|
password: yup.string().min(8).required(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const form = useForm({
|
|
|
|
|
initialValues: {
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
},
|
|
|
|
|
schema: yupResolver(validationSchema),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const signIn = (email: string, password: string) => {
|
2022-10-09 22:30:32 +02:00
|
|
|
authService
|
|
|
|
|
.signIn(email, password)
|
2022-04-25 15:15:17 +02:00
|
|
|
.then(() => window.location.replace("/upload"))
|
2022-10-09 22:30:32 +02:00
|
|
|
.catch((e) => toast.error(e.response.data.message));
|
2022-04-25 15:15:17 +02:00
|
|
|
};
|
|
|
|
|
const signUp = (email: string, password: string) => {
|
2022-10-09 22:30:32 +02:00
|
|
|
authService
|
|
|
|
|
.signUp(email, password)
|
2022-04-25 15:15:17 +02:00
|
|
|
.then(() => signIn(email, password))
|
2022-10-09 22:30:32 +02:00
|
|
|
.catch((e) => toast.error(e.response.data.message));
|
2022-04-25 15:15:17 +02:00
|
|
|
};
|
2022-10-09 22:30:32 +02:00
|
|
|
|
2022-04-25 15:15:17 +02:00
|
|
|
return (
|
|
|
|
|
<Container size={420} my={40}>
|
|
|
|
|
<Title
|
|
|
|
|
align="center"
|
|
|
|
|
sx={(theme) => ({
|
|
|
|
|
fontFamily: `Greycliff CF, ${theme.fontFamily}`,
|
|
|
|
|
fontWeight: 900,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
{mode == "signUp" ? "Sign up" : "Welcome back"}
|
|
|
|
|
</Title>
|
2022-10-09 22:30:32 +02:00
|
|
|
{publicRuntimeConfig.ALLOW_REGISTRATION == "true" && (
|
2022-05-02 11:19:24 +02:00
|
|
|
<Text color="dimmed" size="sm" align="center" mt={5}>
|
|
|
|
|
{mode == "signUp"
|
|
|
|
|
? "You have an account already?"
|
|
|
|
|
: "You don't have an account yet?"}{" "}
|
|
|
|
|
<Anchor href={mode == "signUp" ? "signIn" : "signUp"} size="sm">
|
|
|
|
|
{mode == "signUp" ? "Sign in" : "Sign up"}
|
|
|
|
|
</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) =>
|
|
|
|
|
mode == "signIn"
|
|
|
|
|
? signIn(values.email, values.password)
|
|
|
|
|
: signUp(values.email, values.password)
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<TextInput
|
|
|
|
|
label="Email"
|
|
|
|
|
placeholder="you@email.com"
|
|
|
|
|
{...form.getInputProps("email")}
|
|
|
|
|
/>
|
|
|
|
|
<PasswordInput
|
|
|
|
|
label="Password"
|
|
|
|
|
placeholder="Your password"
|
|
|
|
|
mt="md"
|
|
|
|
|
{...form.getInputProps("password")}
|
|
|
|
|
/>
|
|
|
|
|
<Button fullWidth mt="xl" type="submit">
|
|
|
|
|
{mode == "signUp" ? "Let's get started" : "Sign in"}
|
|
|
|
|
</Button>
|
|
|
|
|
</form>
|
|
|
|
|
</Paper>
|
|
|
|
|
</Container>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default AuthForm;
|