This commit is contained in:
Андрей Дувакин 2024-10-05 10:19:20 +05:00
parent 63a92422fd
commit e345488763
3 changed files with 17 additions and 1 deletions

View File

@ -1,3 +1,11 @@
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');

View File

@ -5,6 +5,10 @@ import { useAuth } from '../AuthContext.jsx';
const PrivateRoute = () => { const PrivateRoute = () => {
const { isAuthenticated } = useAuth(); const { isAuthenticated } = useAuth();
if (isAuthenticated === null) {
return <div>Loading...</div>;
}
return isAuthenticated ? <Outlet /> : <Navigate to="/login" />; return isAuthenticated ? <Outlet /> : <Navigate to="/login" />;
}; };

View File

@ -2,5 +2,9 @@ import React, { useState, useEffect } from 'react';
const Home = () => { const Home = () => {
return return (
<div>
sadvsgrhgrtshte
</div>
)
} }