admin_ruspet/src/pages/Login.tsx

86 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { useState } from 'react'
import {
Container,
Paper,
Title,
Text,
TextInput,
PasswordInput,
Button,
Group,
} from '@mantine/core'
import { useForm } from '@mantine/form'
import { notifications } from '@mantine/notifications'
import { useAuth } from '@/hooks/useAuth'
export default function Login() {
const { login } = useAuth()
const [loading, setLoading] = useState(false)
const form = useForm({
initialValues: {
login: '',
password: '',
},
validate: {
login: (value: string | any[]) => (value.length < 3 ? 'Логин должен содержать минимум 3 символа' : null),
password: (value: string | any[]) => (value.length < 4 ? 'Пароль должен содержать минимум 4 символа' : null),
},
})
const handleSubmit = async (values: typeof form.values) => {
setLoading(true)
try {
await login(values.login, values.password)
notifications.show({
title: 'Успешный вход',
message: `Добро пожаловать, ${values.login}!`,
color: 'green',
})
} catch (error: any) {
notifications.show({
title: 'Ошибка входа',
message: error.response?.data?.error_description || 'Неверный логин или пароль',
color: 'red',
})
} finally {
setLoading(false)
}
}
return (
<Container size={420} my={40}>
<Title ta="center" mb="md">
Корпоративная панель администратора
</Title>
<Text c="dimmed" size="sm" ta="center" mb="xl">
Введите свои данные для входа в систему
</Text>
<Paper withBorder shadow="md" p={30} mt={30} radius="md">
<form onSubmit={form.onSubmit(handleSubmit)}>
<TextInput
label="Логин"
placeholder="admin"
required
{...form.getInputProps('login')}
/>
<PasswordInput
label="Пароль"
placeholder="your password"
required
mt="md"
{...form.getInputProps('password')}
/>
<Group justify="space-between" mt="xl">
<Button type="submit" fullWidth loading={loading}>
Войти
</Button>
</Group>
</form>
</Paper>
</Container>
)
}