._.
This commit is contained in:
parent
b194ec40bf
commit
63a92422fd
@ -6,7 +6,7 @@ const RoutesComponent = () => (
|
||||
<Routes>
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route element={<PrivateRoute />}>
|
||||
|
||||
<Route path='/' />
|
||||
</Route>
|
||||
</Routes>
|
||||
);
|
||||
|
||||
@ -1,11 +1,3 @@
|
||||
import React, { createContext, useContext, useState, useEffect } from 'react';
|
||||
|
||||
const AuthContext = createContext();
|
||||
|
||||
export const useAuth = () => {
|
||||
return useContext(AuthContext);
|
||||
};
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(() => {
|
||||
const savedAuth = localStorage.getItem('isAuthenticated');
|
||||
@ -18,6 +10,8 @@ export const AuthProvider = ({ children }) => {
|
||||
|
||||
const logout = () => {
|
||||
setIsAuthenticated(false);
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('isAuthenticated');
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -30,4 +24,3 @@ export const AuthProvider = ({ children }) => {
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
25
src/api.jsx
25
src/api.jsx
@ -2,14 +2,15 @@ import axios from 'axios';
|
||||
|
||||
const API_URL = 'http://localhost:8000/api';
|
||||
|
||||
const getAuthToken = () => {
|
||||
return localStorage.getItem('token');
|
||||
};
|
||||
|
||||
export const loginUser = async (loginData) => {
|
||||
const params = new URLSearchParams();
|
||||
params.append('grant_type', 'password');
|
||||
params.append('username', loginData.login); // Используем login и password из переданных данных
|
||||
params.append('username', loginData.login);
|
||||
params.append('password', loginData.password);
|
||||
// params.append('scope', '');
|
||||
// params.append('client_id', 'string');
|
||||
// params.append('client_secret', 'string');
|
||||
|
||||
try {
|
||||
const response = await axios.post(`${API_URL}/token`, params, {
|
||||
@ -25,3 +26,19 @@ export const loginUser = async (loginData) => {
|
||||
throw error.response ? error.response.data : error;
|
||||
}
|
||||
};
|
||||
|
||||
export const getAccessories = async () => {
|
||||
try {
|
||||
const token = getAuthToken();
|
||||
const response = await axios.get(`${API_URL}/accessories`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error('Ошибка при запросе аксессуаров:', error.response ? error.response.data : error);
|
||||
throw error.response ? error.response.data : error;
|
||||
}
|
||||
};
|
||||
|
||||
@ -1,17 +1,23 @@
|
||||
import React, { useState } from 'react';
|
||||
import { loginUser } from '../api';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../AuthContext'; // Подключаем AuthContext
|
||||
|
||||
const Login = () => {
|
||||
const [login, setLogin] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const navigate = useNavigate();
|
||||
const { login: authenticate } = useAuth(); // Вытаскиваем метод login из контекста
|
||||
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
const userData = await loginUser({ login, password });
|
||||
|
||||
localStorage.setItem('token', userData.access_token);
|
||||
authenticate();
|
||||
navigate('/');
|
||||
} catch (error) {
|
||||
setError(error.detail ? error.detail : 'Ошибка авторизации');
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user