Files
swiss-datashare/frontend/src/components/auth/SignInForm.tsx

87 lines
2.1 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";
2022-04-25 15:15:17 +02:00
import * as yup from "yup";
import useConfig from "../../hooks/config.hook";
import authService from "../../services/auth.service";
2022-04-25 15:15:17 +02:00
import toast from "../../utils/toast.util";
2022-12-01 23:07:49 +01:00
const SignInForm = () => {
const config = useConfig();
2022-04-25 15:15:17 +02:00
const validationSchema = yup.object().shape({
2022-12-01 23:07:49 +01:00
emailOrUsername: yup.string().required(),
2022-04-25 15:15:17 +02:00
password: yup.string().min(8).required(),
});
const form = useForm({
initialValues: {
2022-12-01 23:07:49 +01:00
emailOrUsername: "",
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 signIn = (email: string, password: string) => {
authService
.signIn(email, password)
2022-12-01 23:07:49 +01:00
.then(() => window.location.replace("/"))
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}>
<Title
align="center"
sx={(theme) => ({
fontFamily: `Greycliff CF, ${theme.fontFamily}`,
fontWeight: 900,
})}
>
2022-12-01 23:07:49 +01:00
Welcome back
2022-04-25 15:15:17 +02:00
</Title>
{config.get("ALLOW_REGISTRATION") && (
2022-05-02 11:19:24 +02:00
<Text color="dimmed" size="sm" align="center" mt={5}>
2022-12-01 23:07:49 +01:00
You don't have an account yet?{" "}
<Anchor component={Link} href={"signUp"} size="sm">
{"Sign up"}
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) =>
2022-12-01 23:07:49 +01:00
signIn(values.emailOrUsername, values.password)
2022-04-25 15:15:17 +02:00
)}
>
<TextInput
2022-12-01 23:07:49 +01:00
label="Email or username"
2022-04-25 15:15:17 +02:00
placeholder="you@email.com"
2022-12-01 23:07:49 +01:00
{...form.getInputProps("emailOrUsername")}
2022-04-25 15:15:17 +02:00
/>
<PasswordInput
label="Password"
placeholder="Your password"
mt="md"
{...form.getInputProps("password")}
/>
<Button fullWidth mt="xl" type="submit">
2022-12-01 23:07:49 +01:00
Sign in
2022-04-25 15:15:17 +02:00
</Button>
</form>
</Paper>
</Container>
);
};
2022-12-01 23:07:49 +01:00
export default SignInForm;