Changeset 702ca77 for reactapp/src/App.js
- Timestamp:
- 08/19/22 19:10:24 (2 years ago)
- Branches:
- main
- Children:
- 6221ab6
- Parents:
- 6eba109
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
reactapp/src/App.js
r6eba109 r702ca77 4 4 import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom"; 5 5 import Home from "./Pages/Home"; 6 import User from "./Pages/User";6 import UserDashboard from "./Pages/UserDashboard"; 7 7 import { useEffect, useState, useMemo } from "react"; 8 8 import AuthApi from "./api/AuthApi"; … … 12 12 const [auth, setAuth] = useState(false); 13 13 const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]); 14 const [authLoaded, setAuthLoaded] = useState(false); 14 15 15 const readCookie = () => {16 const readCookie = async () => { 16 17 const session = Cookies.get("JSESSIONID"); 17 18 if (session) { 18 19 setAuth(true); // go stava true ako postoi takvo cookie (zasto auth=false na sekoe renderiranje) 20 } else { 21 setAuth(false); 19 22 } 23 setAuthLoaded(true); 20 24 }; 21 25 … … 25 29 26 30 const ProtectedRoute = ({ auth, children }) => { 27 if (!auth) { 28 return <Navigate to="/login" replace />; 31 if (authLoaded) { 32 if (!auth) { 33 return <Navigate to="/login" replace />; 34 } 35 return children; 36 } else { 37 return <div>се вчитува cookie...</div>; 29 38 } 30 return children;31 39 }; 32 40 … … 42 50 <Route path="search" element={<SearchResults />}></Route> 43 51 <Route 44 path="user "52 path="user_dashboard" 45 53 element={ 46 54 <ProtectedRoute auth={auth}> 47 <User />55 <UserDashboard /> 48 56 </ProtectedRoute> 49 57 }
Note:
See TracChangeset
for help on using the changeset viewer.