umami/components/pages/login/LoginForm.js

61 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-11-22 07:32:55 +01:00
import { useMutation } from '@tanstack/react-query';
import {
Form,
2023-01-06 07:56:36 +01:00
FormRow,
2022-11-22 07:32:55 +01:00
FormInput,
2020-08-12 07:24:41 +02:00
FormButtons,
2022-11-22 07:32:55 +01:00
TextField,
PasswordField,
SubmitButton,
Icon,
} from 'react-basics';
import { useRouter } from 'next/router';
2022-12-28 05:20:44 +01:00
import useApi from 'hooks/useApi';
import { setUser } from 'store/app';
2022-12-28 05:20:44 +01:00
import { setClientAuthToken } from 'lib/client';
import Logo from 'assets/logo.svg';
2023-01-11 23:47:38 +01:00
import styles from './LoginForm.module.css';
2020-07-24 04:56:55 +02:00
2020-08-07 11:27:12 +02:00
export default function LoginForm() {
2020-10-01 07:34:16 +02:00
const router = useRouter();
2022-11-22 07:32:55 +01:00
const { post } = useApi();
const { mutate, error, isLoading } = useMutation(data => post('/auth/login', data));
const handleSubmit = async data => {
mutate(data, {
2022-11-28 20:14:24 +01:00
onSuccess: async ({ token, user }) => {
2022-12-28 05:20:44 +01:00
setClientAuthToken(token);
2022-11-28 20:14:24 +01:00
setUser(user);
2022-11-22 07:32:55 +01:00
2023-01-11 23:47:38 +01:00
await router.push('/dashboard');
2022-11-22 07:32:55 +01:00
},
2020-10-01 07:34:16 +02:00
});
2020-07-24 04:56:55 +02:00
};
return (
2023-01-11 23:47:38 +01:00
<div className={styles.login}>
<Icon className={styles.icon} size="xl">
<Logo />
</Icon>
<div className={styles.title}>umami</div>
<Form className={styles.form} onSubmit={handleSubmit} error={error}>
2023-01-06 07:56:36 +01:00
<FormRow label="Username">
<FormInput name="username" rules={{ required: 'Required' }}>
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormRow label="Password">
<FormInput name="password" rules={{ required: 'Required' }}>
<PasswordField />
</FormInput>
</FormRow>
2022-11-22 07:32:55 +01:00
<FormButtons>
2023-01-11 23:47:38 +01:00
<SubmitButton className={styles.button} variant="primary" disabled={isLoading}>
2022-11-22 07:32:55 +01:00
Log in
</SubmitButton>
</FormButtons>
</Form>
2023-01-11 23:47:38 +01:00
</div>
2020-07-24 04:56:55 +02:00
);
}