source: reactapp/src/App.js@ 702ca77

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

added current user/logout in header, display karma on user dashboard, started add post functionality in react

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[800779d]1import Professor from "./Pages/Professor";
2import SearchResults from "./Pages/SearchResults";
3import Login from "./Pages/Login";
[6eba109]4import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
[800779d]5import Home from "./Pages/Home";
[702ca77]6import UserDashboard from "./Pages/UserDashboard";
[6eba109]7import { useEffect, useState, useMemo } from "react";
8import AuthApi from "./api/AuthApi";
9import Cookies from "js-cookie";
[7cb8c3c]10
11export default function App() {
[6eba109]12 const [auth, setAuth] = useState(false);
13 const variableAuth = useMemo(() => ({ auth, setAuth }), [auth]);
[702ca77]14 const [authLoaded, setAuthLoaded] = useState(false);
[6eba109]15
[702ca77]16 const readCookie = async () => {
[6eba109]17 const session = Cookies.get("JSESSIONID");
18 if (session) {
19 setAuth(true); // go stava true ako postoi takvo cookie (zasto auth=false na sekoe renderiranje)
[702ca77]20 } else {
21 setAuth(false);
[6eba109]22 }
[702ca77]23 setAuthLoaded(true);
[6eba109]24 };
25
26 useEffect(() => {
27 readCookie();
28 }, []);
29
30 const ProtectedRoute = ({ auth, children }) => {
[702ca77]31 if (authLoaded) {
32 if (!auth) {
33 return <Navigate to="/login" replace />;
34 }
35 return children;
36 } else {
37 return <div>се вчитува cookie...</div>;
[6eba109]38 }
39 };
40
[7cb8c3c]41 return (
[6eba109]42 <AuthApi.Provider value={variableAuth}>
43 <BrowserRouter>
44 <Routes>
45 <Route path="/" element={<Home />}>
46 <Route path="login" element={<Login />}></Route>
47 <Route path="professor">
48 <Route path=":professorId" element={<Professor />} />
49 </Route>
50 <Route path="search" element={<SearchResults />}></Route>
51 <Route
[702ca77]52 path="user_dashboard"
[6eba109]53 element={
54 <ProtectedRoute auth={auth}>
[702ca77]55 <UserDashboard />
[6eba109]56 </ProtectedRoute>
57 }
58 ></Route>
[800779d]59 </Route>
[6eba109]60 </Routes>
61 </BrowserRouter>
62 </AuthApi.Provider>
[7cb8c3c]63 );
[2998dc4]64}
Note: See TracBrowser for help on using the repository browser.