[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";
|
---|
[6eba109] | 6 | import User from "./Pages/User";
|
---|
| 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]);
|
---|
| 14 |
|
---|
| 15 | const readCookie = () => {
|
---|
| 16 | const session = Cookies.get("JSESSIONID");
|
---|
| 17 | if (session) {
|
---|
| 18 | setAuth(true); // go stava true ako postoi takvo cookie (zasto auth=false na sekoe renderiranje)
|
---|
| 19 | }
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 | useEffect(() => {
|
---|
| 23 | readCookie();
|
---|
| 24 | }, []);
|
---|
| 25 |
|
---|
| 26 | const ProtectedRoute = ({ auth, children }) => {
|
---|
| 27 | if (!auth) {
|
---|
| 28 | return <Navigate to="/login" replace />;
|
---|
| 29 | }
|
---|
| 30 | return children;
|
---|
| 31 | };
|
---|
| 32 |
|
---|
[7cb8c3c] | 33 | return (
|
---|
[6eba109] | 34 | <AuthApi.Provider value={variableAuth}>
|
---|
| 35 | <BrowserRouter>
|
---|
| 36 | <Routes>
|
---|
| 37 | <Route path="/" element={<Home />}>
|
---|
| 38 | <Route path="login" element={<Login />}></Route>
|
---|
| 39 | <Route path="professor">
|
---|
| 40 | <Route path=":professorId" element={<Professor />} />
|
---|
| 41 | </Route>
|
---|
| 42 | <Route path="search" element={<SearchResults />}></Route>
|
---|
| 43 | <Route
|
---|
| 44 | path="user"
|
---|
| 45 | element={
|
---|
| 46 | <ProtectedRoute auth={auth}>
|
---|
| 47 | <User />
|
---|
| 48 | </ProtectedRoute>
|
---|
| 49 | }
|
---|
| 50 | ></Route>
|
---|
[800779d] | 51 | </Route>
|
---|
[6eba109] | 52 | </Routes>
|
---|
| 53 | </BrowserRouter>
|
---|
| 54 | </AuthApi.Provider>
|
---|
[7cb8c3c] | 55 | );
|
---|
[2998dc4] | 56 | }
|
---|