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';
|
2022-02-23 07:47:59 +01:00
|
|
|
import { setUser } from 'store/app';
|
2022-12-28 05:20:44 +01:00
|
|
|
import { setClientAuthToken } from 'lib/client';
|
2023-04-08 22:14:22 +02:00
|
|
|
import useMessages from 'hooks/useMessages';
|
2022-02-23 07:47:59 +01:00
|
|
|
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
|
|
|
|
2023-04-21 17:00:42 +02:00
|
|
|
export function LoginForm() {
|
2023-04-08 22:14:22 +02:00
|
|
|
const { formatMessage, labels, getMessage } = useMessages();
|
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>
|
2023-04-08 22:14:22 +02:00
|
|
|
<Form className={styles.form} onSubmit={handleSubmit} error={getMessage(error)}>
|
|
|
|
<FormRow label={formatMessage(labels.username)}>
|
|
|
|
<FormInput name="username" rules={{ required: formatMessage(labels.required) }}>
|
2023-01-06 07:56:36 +01:00
|
|
|
<TextField autoComplete="off" />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
2023-04-08 22:14:22 +02:00
|
|
|
<FormRow label={formatMessage(labels.password)}>
|
|
|
|
<FormInput name="password" rules={{ required: formatMessage(labels.required) }}>
|
2023-01-06 07:56:36 +01:00
|
|
|
<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
|
|
|
);
|
|
|
|
}
|
2023-04-21 17:00:42 +02:00
|
|
|
|
|
|
|
export default LoginForm;
|