umami/src/app/login/LoginForm.tsx

78 lines
2.1 KiB
TypeScript
Raw Normal View History

2022-11-22 07:32:55 +01:00
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';
2023-09-29 14:29:22 +02:00
import { useRouter } from 'next/navigation';
2024-01-29 03:33:40 +01:00
import { useApi, useMessages } from 'components/hooks';
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
2023-04-21 17:00:42 +02:00
export function LoginForm() {
const { formatMessage, labels, getMessage } = useMessages();
2020-10-01 07:34:16 +02:00
const router = useRouter();
2023-12-03 12:07:03 +01:00
const { post, useMutation } = useApi();
const { mutate, error, isPending } = useMutation({
mutationFn: (data: any) => post('/auth/login', data),
});
2022-11-22 07:32:55 +01:00
2024-01-29 03:33:40 +01:00
const handleSubmit = async (data: any) => {
2022-11-22 07:32:55 +01:00
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
2024-01-29 03:33:40 +01:00
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={getMessage(error)}>
<FormRow label={formatMessage(labels.username)}>
2024-02-28 06:41:34 +01:00
<FormInput
data-cy="input-username"
name="username"
rules={{ required: formatMessage(labels.required) }}
>
2023-01-06 07:56:36 +01:00
<TextField autoComplete="off" />
</FormInput>
</FormRow>
<FormRow label={formatMessage(labels.password)}>
2024-02-28 06:41:34 +01:00
<FormInput
data-cy="input-password"
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>
2024-02-28 06:41:34 +01:00
<SubmitButton
data-cy="button-submit"
className={styles.button}
variant="primary"
disabled={isPending}
>
2023-04-21 15:14:14 +02:00
{formatMessage(labels.login)}
2022-11-22 07:32:55 +01:00
</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;