mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
73 lines
1.9 KiB
TypeScript
73 lines
1.9 KiB
TypeScript
import {
|
|
Form,
|
|
FormButtons,
|
|
FormField,
|
|
FormSubmitButton,
|
|
TextField,
|
|
PasswordField,
|
|
Icon,
|
|
Column,
|
|
Heading,
|
|
} from '@umami/react-zen';
|
|
import { useRouter } from 'next/navigation';
|
|
import { useApi, useMessages } from '@/components/hooks';
|
|
import { setUser } from '@/store/app';
|
|
import { setClientAuthToken } from '@/lib/client';
|
|
import Logo from '@/assets/logo.svg';
|
|
|
|
export function LoginForm() {
|
|
const { formatMessage, labels, getMessage } = useMessages();
|
|
const router = useRouter();
|
|
const { post, useMutation } = useApi();
|
|
const { mutate, error, isPending } = useMutation({
|
|
mutationFn: (data: any) => post('/auth/login', data),
|
|
});
|
|
|
|
const handleSubmit = async (data: any) => {
|
|
mutate(data, {
|
|
onSuccess: async ({ token, user }) => {
|
|
setClientAuthToken(token);
|
|
setUser(user);
|
|
|
|
router.push('/dashboard');
|
|
},
|
|
});
|
|
};
|
|
|
|
return (
|
|
<Column justifyContent="center" alignItems="center" padding="8" gap="6">
|
|
<Icon size="lg">
|
|
<Logo />
|
|
</Icon>
|
|
<Heading>umami</Heading>
|
|
<Form onSubmit={handleSubmit} error={getMessage(error)}>
|
|
<FormField
|
|
label={formatMessage(labels.username)}
|
|
data-test="input-username"
|
|
name="username"
|
|
rules={{ required: formatMessage(labels.required) }}
|
|
>
|
|
<TextField autoComplete="off" />
|
|
</FormField>
|
|
<FormField
|
|
label={formatMessage(labels.password)}
|
|
data-test="input-password"
|
|
name="password"
|
|
rules={{ required: formatMessage(labels.required) }}
|
|
>
|
|
<PasswordField />
|
|
</FormField>
|
|
<FormButtons>
|
|
<FormSubmitButton
|
|
data-test="button-submit"
|
|
variant="primary"
|
|
disabled={isPending}
|
|
style={{ flex: 1 }}
|
|
>
|
|
{formatMessage(labels.login)}
|
|
</FormSubmitButton>
|
|
</FormButtons>
|
|
</Form>
|
|
</Column>
|
|
);
|
|
}
|