forked from VinokurovVE/tests
Refactored store
This commit is contained in:
@ -1,12 +1,11 @@
|
||||
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';
|
||||
import { UserData, useAuthStore } from '../../store/auth';
|
||||
import { AxiosResponse } from 'axios';
|
||||
import { SignInFormData, ApiResponse } from '../../interfaces/auth';
|
||||
import { login, setUserData } from '../../store/auth';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import axiosInstance from '../../http/axiosInstance';
|
||||
import AuthService from '../../services/AuthService';
|
||||
import UserService from '../../services/UserService';
|
||||
|
||||
const SignIn = () => {
|
||||
const { register, handleSubmit, formState: { errors } } = useForm<SignInFormData>({
|
||||
@ -20,7 +19,6 @@ const SignIn = () => {
|
||||
}
|
||||
})
|
||||
|
||||
const authStore = useAuthStore();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const onSubmit: SubmitHandler<SignInFormData> = async (data) => {
|
||||
@ -30,21 +28,18 @@ const SignIn = () => {
|
||||
}
|
||||
|
||||
try {
|
||||
const response: AxiosResponse<ApiResponse> = await axiosInstance.post(`/auth/login`, formBody, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
});
|
||||
const response: AxiosResponse<ApiResponse> = await AuthService.login(formBody)
|
||||
console.log('Вход произошел успешно:', response.data);
|
||||
|
||||
const token = response.data.access_token
|
||||
|
||||
const userDataResponse: AxiosResponse<ApiResponse> = await AuthService.getCurrentUser(token)
|
||||
const userDataResponse: AxiosResponse<ApiResponse> = await UserService.getCurrentUser(token)
|
||||
|
||||
console.log('Пользователь:', userDataResponse.data)
|
||||
|
||||
authStore.setUserData(JSON.stringify(userDataResponse.data))
|
||||
authStore.login(token)
|
||||
setUserData(JSON.stringify(userDataResponse.data))
|
||||
|
||||
login(token)
|
||||
|
||||
navigate('/');
|
||||
} catch (error) {
|
||||
@ -63,7 +58,8 @@ const SignIn = () => {
|
||||
fullWidth
|
||||
margin="normal"
|
||||
label="Логин"
|
||||
{...register('username', { required: 'Логин обязателен' })}
|
||||
required
|
||||
{...register('username', { required: 'Введите логин' })}
|
||||
error={!!errors.username}
|
||||
helperText={errors.username?.message}
|
||||
/>
|
||||
@ -72,7 +68,8 @@ const SignIn = () => {
|
||||
margin="normal"
|
||||
type="password"
|
||||
label="Пароль"
|
||||
{...register('password', { required: 'Пароль обязателен' })}
|
||||
required
|
||||
{...register('password', { required: 'Введите пароль' })}
|
||||
error={!!errors.password}
|
||||
helperText={errors.password?.message}
|
||||
/>
|
||||
|
@ -1,9 +1,9 @@
|
||||
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 { SignUpFormData, ApiResponse } from '../../types/auth';
|
||||
import { SignUpFormData, ApiResponse } from '../../interfaces/auth';
|
||||
import axiosInstance from '../../http/axiosInstance';
|
||||
import UserService from '../../services/UserService';
|
||||
|
||||
const SignUp = () => {
|
||||
const { register, handleSubmit, formState: { errors } } = useForm<SignUpFormData>({
|
||||
@ -21,7 +21,7 @@ const SignUp = () => {
|
||||
|
||||
const onSubmit: SubmitHandler<SignUpFormData> = async (data) => {
|
||||
try {
|
||||
const response: AxiosResponse<ApiResponse> = await axiosInstance.post(`${import.meta.env.VITE_API_AUTH_URL}/auth/user`, data);
|
||||
const response: AxiosResponse<ApiResponse> = await UserService.createUser(data)
|
||||
console.log('Успешная регистрация:', response.data);
|
||||
} catch (error) {
|
||||
console.error('Ошибка регистрации:', error);
|
||||
@ -34,23 +34,28 @@ const SignUp = () => {
|
||||
<Typography variant="h4" component="h1" gutterBottom>
|
||||
Регистрация
|
||||
</Typography>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)}>
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
label="Email"
|
||||
required
|
||||
{...register('email', { required: 'Email обязателен' })}
|
||||
error={!!errors.email}
|
||||
helperText={errors.email?.message}
|
||||
/>
|
||||
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
label="Логин"
|
||||
required
|
||||
{...register('login', { required: 'Логин обязателен' })}
|
||||
error={!!errors.login}
|
||||
helperText={errors.login?.message}
|
||||
/>
|
||||
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
@ -59,6 +64,7 @@ const SignUp = () => {
|
||||
error={!!errors.phone}
|
||||
helperText={errors.phone?.message}
|
||||
/>
|
||||
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
@ -67,6 +73,7 @@ const SignUp = () => {
|
||||
error={!!errors.name}
|
||||
helperText={errors.name?.message}
|
||||
/>
|
||||
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
@ -75,15 +82,18 @@ const SignUp = () => {
|
||||
error={!!errors.surname}
|
||||
helperText={errors.surname?.message}
|
||||
/>
|
||||
|
||||
<TextField
|
||||
fullWidth
|
||||
margin="normal"
|
||||
type="password"
|
||||
label="Пароль"
|
||||
required
|
||||
{...register('password', { required: 'Пароль обязателен' })}
|
||||
error={!!errors.password}
|
||||
helperText={errors.password?.message}
|
||||
/>
|
||||
|
||||
<Button type="submit" variant="contained" color="primary">
|
||||
Зарегистрироваться
|
||||
</Button>
|
||||
|
Reference in New Issue
Block a user