|
@ -1,4 +1,5 @@ |
|
|
import React, { useState, ChangeEvent, FormEvent } from 'react'; |
|
|
import React, { useState, ChangeEvent, FormEvent } from 'react'; |
|
|
|
|
|
import { useForm, SubmitHandler } from 'react-hook-form'; |
|
|
import { TextField, Button, Container, Typography, Box } from '@mui/material'; |
|
|
import { TextField, Button, Container, Typography, Box } from '@mui/material'; |
|
|
import axios, { AxiosResponse } from 'axios'; |
|
|
import axios, { AxiosResponse } from 'axios'; |
|
|
import { SignInFormData, ApiResponse } from '../../types/auth'; |
|
|
import { SignInFormData, ApiResponse } from '../../types/auth'; |
|
@ -8,31 +9,24 @@ import axiosInstance from '../../http/axiosInstance'; |
|
|
import AuthService from '../../services/AuthService'; |
|
|
import AuthService from '../../services/AuthService'; |
|
|
|
|
|
|
|
|
const SignIn = () => { |
|
|
const SignIn = () => { |
|
|
const [formData, setFormData] = useState<SignInFormData>({ |
|
|
|
|
|
username: '', |
|
|
|
|
|
password: '', |
|
|
|
|
|
grant_type: 'password', |
|
|
|
|
|
scope: '', |
|
|
|
|
|
client_id: '', |
|
|
|
|
|
client_secret: '' |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
const { register, handleSubmit, formState: { errors } } = useForm<SignInFormData>({ |
|
|
|
|
|
defaultValues: { |
|
|
|
|
|
username: '', |
|
|
|
|
|
password: '', |
|
|
|
|
|
grant_type: 'password', |
|
|
|
|
|
scope: '', |
|
|
|
|
|
client_id: '', |
|
|
|
|
|
client_secret: '' |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
const authStore = useAuthStore(); |
|
|
const authStore = useAuthStore(); |
|
|
|
|
|
|
|
|
const navigate = useNavigate(); |
|
|
const navigate = useNavigate(); |
|
|
|
|
|
|
|
|
const handleChange = (e: ChangeEvent<HTMLInputElement>) => { |
|
|
|
|
|
setFormData({ |
|
|
|
|
|
...formData, |
|
|
|
|
|
[e.target.name]: e.target.value, |
|
|
|
|
|
}); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = async (e: FormEvent) => { |
|
|
|
|
|
e.preventDefault(); |
|
|
|
|
|
|
|
|
const onSubmit: SubmitHandler<SignInFormData> = async (data) => { |
|
|
const formBody = new URLSearchParams(); |
|
|
const formBody = new URLSearchParams(); |
|
|
for (const key in formData) { |
|
|
|
|
|
formBody.append(key, formData[key as keyof SignInFormData] as string); |
|
|
|
|
|
|
|
|
for (const key in data) { |
|
|
|
|
|
formBody.append(key, data[key as keyof SignInFormData] as string); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
@ -42,6 +36,7 @@ const SignIn = () => { |
|
|
}, |
|
|
}, |
|
|
}); |
|
|
}); |
|
|
console.log('Вход произошел успешно:', response.data); |
|
|
console.log('Вход произошел успешно:', response.data); |
|
|
|
|
|
|
|
|
const token = response.data.access_token |
|
|
const token = response.data.access_token |
|
|
|
|
|
|
|
|
const userDataResponse: AxiosResponse<ApiResponse> = await AuthService.getCurrentUser(token) |
|
|
const userDataResponse: AxiosResponse<ApiResponse> = await AuthService.getCurrentUser(token) |
|
@ -49,7 +44,6 @@ const SignIn = () => { |
|
|
console.log('Пользователь:', userDataResponse.data) |
|
|
console.log('Пользователь:', userDataResponse.data) |
|
|
|
|
|
|
|
|
authStore.setUserData(JSON.stringify(userDataResponse.data)) |
|
|
authStore.setUserData(JSON.stringify(userDataResponse.data)) |
|
|
|
|
|
|
|
|
authStore.login(token) |
|
|
authStore.login(token) |
|
|
|
|
|
|
|
|
navigate('/'); |
|
|
navigate('/'); |
|
@ -64,25 +58,23 @@ const SignIn = () => { |
|
|
<Typography variant="h4" component="h1" gutterBottom> |
|
|
<Typography variant="h4" component="h1" gutterBottom> |
|
|
Вход |
|
|
Вход |
|
|
</Typography> |
|
|
</Typography> |
|
|
<form onSubmit={handleSubmit}> |
|
|
|
|
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)}> |
|
|
<TextField |
|
|
<TextField |
|
|
fullWidth |
|
|
fullWidth |
|
|
margin="normal" |
|
|
margin="normal" |
|
|
name="username" |
|
|
|
|
|
label="Логин" |
|
|
label="Логин" |
|
|
value={formData.username} |
|
|
|
|
|
onChange={handleChange} |
|
|
|
|
|
required |
|
|
|
|
|
|
|
|
{...register('username', { required: 'Логин обязателен' })} |
|
|
|
|
|
error={!!errors.username} |
|
|
|
|
|
helperText={errors.username?.message} |
|
|
/> |
|
|
/> |
|
|
<TextField |
|
|
<TextField |
|
|
fullWidth |
|
|
fullWidth |
|
|
margin="normal" |
|
|
margin="normal" |
|
|
type="password" |
|
|
type="password" |
|
|
name="password" |
|
|
|
|
|
label="Пароль" |
|
|
label="Пароль" |
|
|
value={formData.password} |
|
|
|
|
|
onChange={handleChange} |
|
|
|
|
|
required |
|
|
|
|
|
|
|
|
{...register('password', { required: 'Пароль обязателен' })} |
|
|
|
|
|
error={!!errors.password} |
|
|
|
|
|
helperText={errors.password?.message} |
|
|
/> |
|
|
/> |
|
|
<Button type="submit" variant="contained" color="primary"> |
|
|
<Button type="submit" variant="contained" color="primary"> |
|
|
Вход |
|
|
Вход |
|
|