umami/components/forms/LoginForm.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-11-22 07:32:55 +01:00
import { useMutation } from '@tanstack/react-query';
import {
Form,
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';
2022-11-22 07:32:55 +01:00
import styles from './Form.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
await router.push('/websites');
},
2020-10-01 07:34:16 +02:00
});
2020-07-24 04:56:55 +02:00
};
return (
2022-11-22 07:32:55 +01:00
<>
<div className={styles.header}>
<Icon size="xl">
<Logo />
</Icon>
<p>umami</p>
</div>
2022-11-28 20:14:24 +01:00
<Form className={styles.form} onSubmit={handleSubmit} error={error}>
2022-11-22 07:32:55 +01:00
<FormInput name="username" label="Username" rules={{ required: 'Required' }}>
<TextField autoComplete="off" />
</FormInput>
<FormInput name="password" label="Password" rules={{ required: 'Required' }}>
<PasswordField />
</FormInput>
<FormButtons>
<SubmitButton variant="primary" className={styles.button} disabled={isLoading}>
Log in
</SubmitButton>
</FormButtons>
</Form>
</>
2020-07-24 04:56:55 +02:00
);
}