._.
This commit is contained in:
parent
d9ab6ac43c
commit
e2ceb53c89
@ -11,19 +11,19 @@ export const AuthProvider = ({ children }) => {
|
||||
const savedAuth = localStorage.getItem("isAuthenticated");
|
||||
return savedAuth === "true";
|
||||
});
|
||||
const [user, setUser] = useState(null);
|
||||
|
||||
const login = (userData) => {
|
||||
setIsAuthenticated(true);
|
||||
setUser(userData);
|
||||
localStorage.setItem("token", userData.token);
|
||||
localStorage.setItem("user", JSON.stringify(userData.user));
|
||||
localStorage.setItem("isAuthenticated", true);
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
setIsAuthenticated(false);
|
||||
setUser(null);
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("isAuthenticated");
|
||||
localStorage.removeItem("user");
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@ -31,7 +31,7 @@ export const AuthProvider = ({ children }) => {
|
||||
}, [isAuthenticated]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ isAuthenticated, user, login, logout }}>
|
||||
<AuthContext.Provider value={{ isAuthenticated, login, logout }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
||||
25
src/api.jsx
25
src/api.jsx
@ -19,7 +19,11 @@ export const loginUser = async (loginData) => {
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
|
||||
return {
|
||||
token: response.data.access_token,
|
||||
user: response.data.user,
|
||||
};
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"Ошибка при запросе:",
|
||||
@ -28,22 +32,3 @@ 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;
|
||||
}
|
||||
};
|
||||
|
||||
@ -3,8 +3,9 @@ import { Link, useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "../AuthContext";
|
||||
|
||||
const Header = () => {
|
||||
const { isAuthenticated, logout, user } = useAuth();
|
||||
const { isAuthenticated, logout } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const user = JSON.parse(localStorage.getItem("user"));
|
||||
|
||||
const handleLogout = () => {
|
||||
logout();
|
||||
@ -29,7 +30,7 @@ const Header = () => {
|
||||
<div className="d-flex align-items-center ml-auto">
|
||||
{isAuthenticated ? (
|
||||
<div className="nav-item d-flex align-items-center">
|
||||
<span className="nav-link">{user?.user.login}</span>{" "}
|
||||
<span className="nav-link">{user?.login}</span>{" "}
|
||||
<button
|
||||
className="btn btn-outline-danger"
|
||||
onClick={handleLogout}
|
||||
|
||||
@ -1,9 +1,7 @@
|
||||
// app/src/pages/Login.jsx
|
||||
|
||||
import React, { useState } from "react";
|
||||
import { loginUser } from "../api";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "../AuthContext"; // Импортируйте useAuth
|
||||
import { useAuth } from "../AuthContext";
|
||||
|
||||
const Login = () => {
|
||||
const [login, setLogin] = useState("");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user