[800779d] | 1 | import Professor from "./Pages/Professor";
|
---|
| 2 | import SearchResults from "./Pages/SearchResults";
|
---|
| 3 | import Login from "./Pages/Login";
|
---|
[cae16b5] | 4 | import Registration from "./Pages/Registration";
|
---|
[6eba109] | 5 | import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
---|
[800779d] | 6 | import Home from "./Pages/Home";
|
---|
[702ca77] | 7 | import UserDashboard from "./Pages/UserDashboard";
|
---|
[6eba109] | 8 | import { useEffect, useState, useMemo } from "react";
|
---|
| 9 | import AuthApi from "./api/AuthApi";
|
---|
| 10 | import Cookies from "js-cookie";
|
---|
[6221ab6] | 11 | import axios from "./api/axios";
|
---|
| 12 | import JSOG from "jsog";
|
---|
[7cb8c3c] | 13 |
|
---|
| 14 | export default function App() {
|
---|
[6eba109] | 15 | const [auth, setAuth] = useState(false);
|
---|
[6221ab6] | 16 | const [user, setUser] = useState(null);
|
---|
| 17 | const [userLoaded, setUserLoaded] = useState(false);
|
---|
[6eba109] | 18 | const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]);
|
---|
[702ca77] | 19 | const [authLoaded, setAuthLoaded] = useState(false);
|
---|
[6eba109] | 20 |
|
---|
[6221ab6] | 21 | const fetchUser = async () => {
|
---|
| 22 | try {
|
---|
| 23 | const response = await axios.get(
|
---|
[62b653f] | 24 | "http://192.168.0.17:8080/secure/currentUser",
|
---|
[6221ab6] | 25 | { withCredentials: true }
|
---|
| 26 | );
|
---|
| 27 | var cyclicGraph = await response.data;
|
---|
| 28 | var jsogStructure = JSOG.encode(cyclicGraph);
|
---|
| 29 | cyclicGraph = JSOG.decode(jsogStructure);
|
---|
| 30 | setUser(cyclicGraph);
|
---|
| 31 | setUserLoaded(true);
|
---|
| 32 | } catch (error) {
|
---|
| 33 | console.log("Fetching error", error);
|
---|
| 34 | }
|
---|
| 35 | };
|
---|
| 36 |
|
---|
[702ca77] | 37 | const readCookie = async () => {
|
---|
[6eba109] | 38 | const session = Cookies.get("JSESSIONID");
|
---|
| 39 | if (session) {
|
---|
[cae16b5] | 40 | setAuth(true);
|
---|
[6221ab6] | 41 | fetchUser();
|
---|
[702ca77] | 42 | } else {
|
---|
| 43 | setAuth(false);
|
---|
[6eba109] | 44 | }
|
---|
[702ca77] | 45 | setAuthLoaded(true);
|
---|
[6eba109] | 46 | };
|
---|
| 47 |
|
---|
| 48 | useEffect(() => {
|
---|
| 49 | readCookie();
|
---|
| 50 | }, []);
|
---|
| 51 |
|
---|
| 52 | const ProtectedRoute = ({ auth, children }) => {
|
---|
[702ca77] | 53 | if (authLoaded) {
|
---|
| 54 | if (!auth) {
|
---|
| 55 | return <Navigate to="/login" replace />;
|
---|
| 56 | }
|
---|
| 57 | return children;
|
---|
| 58 | } else {
|
---|
| 59 | return <div>се вчитува cookie...</div>;
|
---|
[6eba109] | 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
[7cb8c3c] | 63 | return (
|
---|
[6eba109] | 64 | <AuthApi.Provider value={variableAuth}>
|
---|
| 65 | <BrowserRouter>
|
---|
| 66 | <Routes>
|
---|
[6221ab6] | 67 | <Route
|
---|
| 68 | path="/"
|
---|
| 69 | element={<Home user={user} userLoaded={userLoaded} />}
|
---|
| 70 | >
|
---|
[6eba109] | 71 | <Route path="login" element={<Login />}></Route>
|
---|
[cae16b5] | 72 | <Route path="registration" element={<Registration />}></Route>
|
---|
[6eba109] | 73 | <Route path="professor">
|
---|
[6221ab6] | 74 | <Route
|
---|
| 75 | path=":professorId"
|
---|
| 76 | element={<Professor user={user} userLoaded={userLoaded} />}
|
---|
| 77 | />
|
---|
[6eba109] | 78 | </Route>
|
---|
| 79 | <Route path="search" element={<SearchResults />}></Route>
|
---|
| 80 | <Route
|
---|
[702ca77] | 81 | path="user_dashboard"
|
---|
[6eba109] | 82 | element={
|
---|
| 83 | <ProtectedRoute auth={auth}>
|
---|
[6221ab6] | 84 | {<UserDashboard user={user} userLoaded={userLoaded} />}
|
---|
[6eba109] | 85 | </ProtectedRoute>
|
---|
| 86 | }
|
---|
| 87 | ></Route>
|
---|
[800779d] | 88 | </Route>
|
---|
[6eba109] | 89 | </Routes>
|
---|
| 90 | </BrowserRouter>
|
---|
| 91 | </AuthApi.Provider>
|
---|
[7cb8c3c] | 92 | );
|
---|
[2998dc4] | 93 | }
|
---|