diff --git a/frontend_reactjs/src/pages/auth/SignIn.tsx b/frontend_reactjs/src/pages/auth/SignIn.tsx index 861fbcc..275d698 100644 --- a/frontend_reactjs/src/pages/auth/SignIn.tsx +++ b/frontend_reactjs/src/pages/auth/SignIn.tsx @@ -1,4 +1,5 @@ import React, { useState, ChangeEvent, FormEvent } from 'react'; +import { useForm, SubmitHandler } from 'react-hook-form'; import { TextField, Button, Container, Typography, Box } from '@mui/material'; import axios, { AxiosResponse } from 'axios'; import { SignInFormData, ApiResponse } from '../../types/auth'; @@ -8,31 +9,24 @@ import axiosInstance from '../../http/axiosInstance'; import AuthService from '../../services/AuthService'; const SignIn = () => { - const [formData, setFormData] = useState({ - username: '', - password: '', - grant_type: 'password', - scope: '', - client_id: '', - client_secret: '' - }); + const { register, handleSubmit, formState: { errors } } = useForm({ + defaultValues: { + username: '', + password: '', + grant_type: 'password', + scope: '', + client_id: '', + client_secret: '' + } + }) const authStore = useAuthStore(); - const navigate = useNavigate(); - const handleChange = (e: ChangeEvent) => { - setFormData({ - ...formData, - [e.target.name]: e.target.value, - }); - }; - - const handleSubmit = async (e: FormEvent) => { - e.preventDefault(); + const onSubmit: SubmitHandler = async (data) => { 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 { @@ -42,6 +36,7 @@ const SignIn = () => { }, }); console.log('Вход произошел успешно:', response.data); + const token = response.data.access_token const userDataResponse: AxiosResponse = await AuthService.getCurrentUser(token) @@ -49,7 +44,6 @@ const SignIn = () => { console.log('Пользователь:', userDataResponse.data) authStore.setUserData(JSON.stringify(userDataResponse.data)) - authStore.login(token) navigate('/'); @@ -64,25 +58,23 @@ const SignIn = () => { Вход -
+