source: reactapp/src/App.js@ 8d83180

main
Last change on this file since 8d83180 was 8d83180, checked in by unknown <mlviktor23@…>, 2 years ago

forums/page nav

  • Property mode set to 100644
File size: 3.1 KB
Line 
1import Professor from "./Pages/Professor";
2import SearchResults from "./Pages/SearchResults";
3import Login from "./Pages/Login";
4import Registration from "./Pages/Registration";
5import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
6import Home from "./Pages/Home";
7import UserDashboard from "./Pages/UserDashboard";
8import Subject from "./Pages/Subject";
9import University from "./Pages/University";
10import Faculty from "./Pages/Faculty";
11import { useEffect, useState, useMemo } from "react";
12import AuthApi from "./api/AuthApi";
13import Cookies from "js-cookie";
14import axios from "./api/axios";
15import JSOG from "jsog";
16import NotFound from "./Pages/NotFound";
17
18export default function App() {
19 const [auth, setAuth] = useState(false);
20 const [user, setUser] = useState(null);
21 const [userLoaded, setUserLoaded] = useState(false);
22 const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]);
23 const [authLoaded, setAuthLoaded] = useState(false);
24
25 const fetchUser = async () => {
26 try {
27 const response = await axios.get(
28 "http://192.168.0.17:8080/secure/currentUser",
29 { withCredentials: true }
30 );
31 var cyclicGraph = await response.data;
32 var jsogStructure = JSOG.encode(cyclicGraph);
33 cyclicGraph = JSOG.decode(jsogStructure);
34 setUser(cyclicGraph);
35 setUserLoaded(true);
36 } catch (error) {
37 console.log("Fetching error", error);
38 }
39 };
40
41 const readCookie = async () => {
42 const session = Cookies.get("JSESSIONID");
43 if (session) {
44 setAuth(true);
45 fetchUser();
46 } else {
47 setAuth(false);
48 }
49 setAuthLoaded(true);
50 };
51
52 useEffect(() => {
53 readCookie();
54 }, []);
55
56 const ProtectedRoute = ({ auth, children }) => {
57 if (authLoaded) {
58 if (!auth) {
59 return <Navigate to="/login" replace />;
60 }
61 return children;
62 } else {
63 return <div>се вчитува cookie...</div>;
64 }
65 };
66
67 return (
68 <AuthApi.Provider value={variableAuth}>
69 <BrowserRouter>
70 <Routes>
71 <Route
72 path="/"
73 element={<Home user={user} userLoaded={userLoaded} />}
74 >
75 <Route path="login" element={<Login />}></Route>
76 <Route path="registration" element={<Registration />}></Route>
77 <Route path="professor">
78 <Route
79 path=":professorId"
80 element={<Professor user={user} userLoaded={userLoaded} />}
81 />
82 </Route>
83 <Route path="university/:universityId" element={<University />} />
84 <Route path="faculty/:facultyId" element={<Faculty />} />
85 <Route path="subject/:subjectId" element={<Subject />} />
86 <Route path="search" element={<SearchResults />}></Route>
87 <Route
88 path="user_dashboard"
89 element={
90 <ProtectedRoute auth={auth}>
91 {<UserDashboard user={user} userLoaded={userLoaded} />}
92 </ProtectedRoute>
93 }
94 ></Route>
95 </Route>
96 <Route path="*" element={<NotFound />} />
97 </Routes>
98 </BrowserRouter>
99 </AuthApi.Provider>
100 );
101}
Note: See TracBrowser for help on using the repository browser.