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