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