[800779d] | 1 | import Professor from "./Pages/Professor";
|
---|
| 2 | import SearchResults from "./Pages/SearchResults";
|
---|
| 3 | import Login from "./Pages/Login";
|
---|
[6eba109] | 4 | import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
|
---|
[800779d] | 5 | import Home from "./Pages/Home";
|
---|
[702ca77] | 6 | import UserDashboard from "./Pages/UserDashboard";
|
---|
[6eba109] | 7 | import { useEffect, useState, useMemo } from "react";
|
---|
| 8 | import AuthApi from "./api/AuthApi";
|
---|
| 9 | import Cookies from "js-cookie";
|
---|
[7cb8c3c] | 10 |
|
---|
| 11 | export 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 | }
|
---|