Changeset 6eba109 for reactapp/src
- Timestamp:
- 08/17/22 16:21:10 (2 years ago)
- Branches:
- main
- Children:
- 702ca77
- Parents:
- 800779d
- Location:
- reactapp/src
- Files:
-
- 3 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
reactapp/src/App.js
r800779d r6eba109 2 2 import SearchResults from "./Pages/SearchResults"; 3 3 import Login from "./Pages/Login"; 4 import { BrowserRouter, Routes, Route } from "react-router-dom";4 import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom"; 5 5 import Home from "./Pages/Home"; 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"; 6 10 7 11 export default function App() { 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 8 33 return ( 9 <BrowserRouter> 10 <Routes> 11 <Route path="/" element={<Home />}> 12 <Route path="login" element={<Login />}></Route> 13 <Route path="professor"> 14 <Route path=":professorId" element={<Professor />} /> 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> 15 51 </Route> 16 <Route path="search" element={<SearchResults />}></Route> 17 </Route> 18 </Routes> 19 </BrowserRouter> 52 </Routes> 53 </BrowserRouter> 54 </AuthApi.Provider> 20 55 ); 21 56 } -
reactapp/src/Components/Search.js
r800779d r6eba109 15 15 16 16 useEffect(() => { 17 const url = `http://192.168.0.1 7:8080/public/professors/nameContains/${transliterate(17 const url = `http://192.168.0.18:8080/public/professors/nameContains/${transliterate( 18 18 query 19 19 )}`; -
reactapp/src/Pages/Home.js
r800779d r6eba109 20 20 </a>{" "} 21 21 <Search /> 22 <div style={{ marginTop: "140px" }}></div> 22 23 <Outlet /> 23 24 </MainWrapper> -
reactapp/src/Pages/Login.js
r800779d r6eba109 1 import React, { useRef, useState, useEffect } from "react"; 1 import React, { useRef, useState, useEffect, useContext } from "react"; 2 import { Navigate } from "react-router-dom"; 3 import AuthApi from "../api/AuthApi"; 2 4 import axios from "../api/axios"; 5 import Cookies from "js-cookie"; 3 6 const LOGIN_URL = "/login"; 4 7 5 8 const Login = () => { 9 const { auth, setAuth } = useContext(AuthApi); 6 10 const userRef = useRef(); 7 11 const errRef = useRef(); … … 10 14 const [password, setPassword] = useState(""); 11 15 const [errMsg, setErrMsg] = useState(""); 12 const [success, setSuccess] = useState(false);13 16 14 17 useEffect(() => { 15 18 userRef.current.focus(); 16 19 }, []); 17 18 useEffect(() => {19 setErrMsg("");20 }, [username, password]);21 20 22 21 const handleSubmit = async (e) => { … … 33 32 } 34 33 ); 34 if (!response.request.responseURL.includes("error")) { 35 // ako NE redirektira na /login?error 36 Cookies.set("JSESSIONID", response.data.sessionId); 37 setAuth(true); 38 setErrMsg(""); 39 } else { 40 setErrMsg("Погрешно корисиничко име и/или лозинка"); 41 } 42 35 43 setUsername(""); 36 44 setPassword(""); 37 setSuccess(true);38 45 }; 39 46 40 return success ? ( 47 const handleLogout = () => { 48 setAuth(false); 49 Cookies.remove("JSESSIONID"); 50 }; 51 52 return auth ? ( 53 /* 41 54 <div style={{ marginTop: "140px" }}> 42 55 <h1>Успешна најава!</h1> 43 56 <br /> 44 57 <p> 45 <a href="/ ">Оди на почетната страница</a>58 <a href="/user">Оди на protected</a> 46 59 </p> 60 <button onClick={handleLogout}>Одјави се</button> 47 61 </div> 62 */ 63 <Navigate to="/user" /> 48 64 ) : ( 49 65 <div style={{ marginTop: "140px" }}> -
reactapp/src/Pages/Professor.js
r800779d r6eba109 18 18 19 19 useEffect(() => { 20 const url = `http://192.168.0.1 7:8080/public/professor/${params.professorId}`;20 const url = `http://192.168.0.18:8080/public/professor/${params.professorId}`; 21 21 22 22 const fetchData = async () => { -
reactapp/src/api/axios.js
r800779d r6eba109 2 2 3 3 export default axios.create({ 4 baseURL: "http://192.168.0.1 7:8080",4 baseURL: "http://192.168.0.18:8080", 5 5 });
Note:
See TracChangeset
for help on using the changeset viewer.