forked from VinokurovVE/tests
Auth: react-hook-form
This commit is contained in:
@ -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>({
|
const { register, handleSubmit, formState: { errors } } = useForm<SignInFormData>({
|
||||||
username: '',
|
defaultValues: {
|
||||||
password: '',
|
username: '',
|
||||||
grant_type: 'password',
|
password: '',
|
||||||
scope: '',
|
grant_type: 'password',
|
||||||
client_id: '',
|
scope: '',
|
||||||
client_secret: ''
|
client_id: '',
|
||||||
});
|
client_secret: ''
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const authStore = useAuthStore();
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
const onSubmit: SubmitHandler<SignInFormData> = async (data) => {
|
||||||
setFormData({
|
|
||||||
...formData,
|
|
||||||
[e.target.name]: e.target.value,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async (e: FormEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
const formBody = new URLSearchParams();
|
const formBody = new URLSearchParams();
|
||||||
for (const key in formData) {
|
for (const key in data) {
|
||||||
formBody.append(key, formData[key as keyof SignInFormData] as string);
|
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}
|
{...register('username', { required: 'Логин обязателен' })}
|
||||||
onChange={handleChange}
|
error={!!errors.username}
|
||||||
required
|
helperText={errors.username?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
type="password"
|
type="password"
|
||||||
name="password"
|
|
||||||
label="Пароль"
|
label="Пароль"
|
||||||
value={formData.password}
|
{...register('password', { required: 'Пароль обязателен' })}
|
||||||
onChange={handleChange}
|
error={!!errors.password}
|
||||||
required
|
helperText={errors.password?.message}
|
||||||
/>
|
/>
|
||||||
<Button type="submit" variant="contained" color="primary">
|
<Button type="submit" variant="contained" color="primary">
|
||||||
Вход
|
Вход
|
||||||
|
@ -1,33 +1,27 @@
|
|||||||
// src/components/SignUp.tsx
|
|
||||||
|
|
||||||
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 { SignUpFormData, ApiResponse } from '../../types/auth';
|
import { SignUpFormData, ApiResponse } from '../../types/auth';
|
||||||
import axiosInstance from '../../http/axiosInstance';
|
import axiosInstance from '../../http/axiosInstance';
|
||||||
|
|
||||||
const SignUp: React.FC = () => {
|
const SignUp = () => {
|
||||||
const [formData, setFormData] = useState<SignUpFormData>({
|
const { register, handleSubmit, formState: { errors } } = useForm<SignUpFormData>({
|
||||||
email: '',
|
defaultValues: {
|
||||||
login: '',
|
email: '',
|
||||||
phone: '',
|
login: '',
|
||||||
name: '',
|
phone: '',
|
||||||
surname: '',
|
name: '',
|
||||||
is_active: true,
|
surname: '',
|
||||||
password: '',
|
is_active: true,
|
||||||
});
|
password: '',
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
||||||
setFormData({
|
|
||||||
...formData,
|
|
||||||
[e.target.name]: e.target.value,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleSubmit = async (e: FormEvent) => {
|
const onSubmit: SubmitHandler<SignUpFormData> = async (data) => {
|
||||||
e.preventDefault();
|
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse<ApiResponse> = await axiosInstance.post(`${import.meta.env.VITE_API_AUTH_URL}/auth/user`, formData);
|
const response: AxiosResponse<ApiResponse> = await axiosInstance.post(`${import.meta.env.VITE_API_AUTH_URL}/auth/user`, data);
|
||||||
console.log('Успешная регистрация:', response.data);
|
console.log('Успешная регистрация:', response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Ошибка регистрации:', error);
|
console.error('Ошибка регистрации:', error);
|
||||||
@ -40,58 +34,55 @@ const SignUp: React.FC = () => {
|
|||||||
<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="email"
|
|
||||||
label="Email"
|
label="Email"
|
||||||
value={formData.email}
|
{...register('email', { required: 'Email обязателен' })}
|
||||||
onChange={handleChange}
|
error={!!errors.email}
|
||||||
required
|
helperText={errors.email?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
name="login"
|
|
||||||
label="Логин"
|
label="Логин"
|
||||||
value={formData.login}
|
{...register('login', { required: 'Логин обязателен' })}
|
||||||
onChange={handleChange}
|
error={!!errors.login}
|
||||||
required
|
helperText={errors.login?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
name="phone"
|
|
||||||
label="Телефон"
|
label="Телефон"
|
||||||
value={formData.phone}
|
{...register('phone')}
|
||||||
onChange={handleChange}
|
error={!!errors.phone}
|
||||||
|
helperText={errors.phone?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
name="name"
|
|
||||||
label="Имя"
|
label="Имя"
|
||||||
value={formData.name}
|
{...register('name')}
|
||||||
onChange={handleChange}
|
error={!!errors.name}
|
||||||
|
helperText={errors.name?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
name="surname"
|
|
||||||
label="Фамилия"
|
label="Фамилия"
|
||||||
value={formData.surname}
|
{...register('surname')}
|
||||||
onChange={handleChange}
|
error={!!errors.surname}
|
||||||
|
helperText={errors.surname?.message}
|
||||||
/>
|
/>
|
||||||
<TextField
|
<TextField
|
||||||
fullWidth
|
fullWidth
|
||||||
margin="normal"
|
margin="normal"
|
||||||
type="password"
|
type="password"
|
||||||
name="password"
|
|
||||||
label="Пароль"
|
label="Пароль"
|
||||||
value={formData.password}
|
{...register('password', { required: 'Пароль обязателен' })}
|
||||||
onChange={handleChange}
|
error={!!errors.password}
|
||||||
required
|
helperText={errors.password?.message}
|
||||||
/>
|
/>
|
||||||
<Button type="submit" variant="contained" color="primary">
|
<Button type="submit" variant="contained" color="primary">
|
||||||
Зарегистрироваться
|
Зарегистрироваться
|
||||||
|
Reference in New Issue
Block a user