source: reactapp/src/App.js@ c68150f

main
Last change on this file since c68150f was c68150f, checked in by unknown <mlviktor23@…>, 21 months ago

left: moderation, oAuth, messaging

  • Property mode set to 100644
File size: 2.4 KB
RevLine 
[800779d]1import Professor from "./Pages/Professor";
2import SearchResults from "./Pages/SearchResults";
3import Login from "./Pages/Login";
[cae16b5]4import Registration from "./Pages/Registration";
[6eba109]5import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
[800779d]6import Home from "./Pages/Home";
[702ca77]7import UserDashboard from "./Pages/UserDashboard";
[8d83180]8import Subject from "./Pages/Subject";
9import University from "./Pages/University";
10import Faculty from "./Pages/Faculty";
[6eba109]11import { useEffect, useState, useMemo } from "react";
12import AuthApi from "./api/AuthApi";
13import Cookies from "js-cookie";
[8d83180]14import NotFound from "./Pages/NotFound";
[c68150f]15import Topic from "./Pages/Topic";
[7cb8c3c]16
17export default function App() {
[6eba109]18 const [auth, setAuth] = useState(false);
19 const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]);
[702ca77]20 const [authLoaded, setAuthLoaded] = useState(false);
[6eba109]21
[702ca77]22 const readCookie = async () => {
[6eba109]23 const session = Cookies.get("JSESSIONID");
24 if (session) {
[cae16b5]25 setAuth(true);
[702ca77]26 } else {
27 setAuth(false);
[6eba109]28 }
[702ca77]29 setAuthLoaded(true);
[6eba109]30 };
31
32 useEffect(() => {
[c68150f]33 document.title = "profesori.mk";
[6eba109]34 readCookie();
35 }, []);
36
37 const ProtectedRoute = ({ auth, children }) => {
[702ca77]38 if (authLoaded) {
39 if (!auth) {
40 return <Navigate to="/login" replace />;
41 }
42 return children;
43 } else {
44 return <div>се вчитува cookie...</div>;
[6eba109]45 }
46 };
47
[7cb8c3c]48 return (
[6eba109]49 <AuthApi.Provider value={variableAuth}>
50 <BrowserRouter>
51 <Routes>
[c68150f]52 <Route path="/" element={<Home />}>
[6eba109]53 <Route path="login" element={<Login />}></Route>
[cae16b5]54 <Route path="registration" element={<Registration />}></Route>
[6eba109]55 <Route path="professor">
[c68150f]56 <Route path=":professorId" element={<Professor />} />
[6eba109]57 </Route>
[8d83180]58 <Route path="university/:universityId" element={<University />} />
59 <Route path="faculty/:facultyId" element={<Faculty />} />
60 <Route path="subject/:subjectId" element={<Subject />} />
[c68150f]61 <Route path="topic/:topicId" element={<Topic />} />
[6eba109]62 <Route path="search" element={<SearchResults />}></Route>
63 <Route
[702ca77]64 path="user_dashboard"
[6eba109]65 element={
[c68150f]66 <ProtectedRoute auth={auth}>{<UserDashboard />}</ProtectedRoute>
[6eba109]67 }
68 ></Route>
[800779d]69 </Route>
[8d83180]70 <Route path="*" element={<NotFound />} />
[6eba109]71 </Routes>
72 </BrowserRouter>
73 </AuthApi.Provider>
[7cb8c3c]74 );
[2998dc4]75}
Note: See TracBrowser for help on using the repository browser.