1 | import Professor from "./Pages/Professor";
|
---|
2 | import SearchResults from "./Pages/SearchResults";
|
---|
3 | import Login from "./Pages/Login";
|
---|
4 | import Registration from "./Pages/Registration";
|
---|
5 | import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
---|
6 | import Home from "./Pages/Home";
|
---|
7 | import UserDashboard from "./Pages/UserDashboard";
|
---|
8 | import Subject from "./Pages/Subject";
|
---|
9 | import University from "./Pages/University";
|
---|
10 | import Faculty from "./Pages/Faculty";
|
---|
11 | import { useEffect, useState, useMemo } from "react";
|
---|
12 | import AuthApi from "./api/AuthApi";
|
---|
13 | import Cookies from "js-cookie";
|
---|
14 | import axios from "./api/axios";
|
---|
15 | import JSOG from "jsog";
|
---|
16 | import NotFound from "./Pages/NotFound";
|
---|
17 |
|
---|
18 | export 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 | }
|
---|