Changeset 6eba109 for reactapp


Ignore:
Timestamp:
08/17/22 16:21:10 (22 months ago)
Author:
unknown <mlviktor23@…>
Branches:
main
Children:
702ca77
Parents:
800779d
Message:

implemented authentication in react

Location:
reactapp/src
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • reactapp/src/App.js

    r800779d r6eba109  
    22import SearchResults from "./Pages/SearchResults";
    33import Login from "./Pages/Login";
    4 import { BrowserRouter, Routes, Route } from "react-router-dom";
     4import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
    55import Home from "./Pages/Home";
     6import User from "./Pages/User";
     7import { useEffect, useState, useMemo } from "react";
     8import AuthApi from "./api/AuthApi";
     9import Cookies from "js-cookie";
    610
    711export 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
    833  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>
    1551          </Route>
    16           <Route path="search" element={<SearchResults />}></Route>
    17         </Route>
    18       </Routes>
    19     </BrowserRouter>
     52        </Routes>
     53      </BrowserRouter>
     54    </AuthApi.Provider>
    2055  );
    2156}
  • reactapp/src/Components/Search.js

    r800779d r6eba109  
    1515
    1616  useEffect(() => {
    17     const url = `http://192.168.0.17:8080/public/professors/nameContains/${transliterate(
     17    const url = `http://192.168.0.18:8080/public/professors/nameContains/${transliterate(
    1818      query
    1919    )}`;
  • reactapp/src/Pages/Home.js

    r800779d r6eba109  
    2020      </a>{" "}
    2121      <Search />
     22      <div style={{ marginTop: "140px" }}></div>
    2223      <Outlet />
    2324    </MainWrapper>
  • reactapp/src/Pages/Login.js

    r800779d r6eba109  
    1 import React, { useRef, useState, useEffect } from "react";
     1import React, { useRef, useState, useEffect, useContext } from "react";
     2import { Navigate } from "react-router-dom";
     3import AuthApi from "../api/AuthApi";
    24import axios from "../api/axios";
     5import Cookies from "js-cookie";
    36const LOGIN_URL = "/login";
    47
    58const Login = () => {
     9  const { auth, setAuth } = useContext(AuthApi);
    610  const userRef = useRef();
    711  const errRef = useRef();
     
    1014  const [password, setPassword] = useState("");
    1115  const [errMsg, setErrMsg] = useState("");
    12   const [success, setSuccess] = useState(false);
    1316
    1417  useEffect(() => {
    1518    userRef.current.focus();
    1619  }, []);
    17 
    18   useEffect(() => {
    19     setErrMsg("");
    20   }, [username, password]);
    2120
    2221  const handleSubmit = async (e) => {
     
    3332      }
    3433    );
     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
    3543    setUsername("");
    3644    setPassword("");
    37     setSuccess(true);
    3845  };
    3946
    40   return success ? (
     47  const handleLogout = () => {
     48    setAuth(false);
     49    Cookies.remove("JSESSIONID");
     50  };
     51
     52  return auth ? (
     53    /*
    4154    <div style={{ marginTop: "140px" }}>
    4255      <h1>Успешна најава!</h1>
    4356      <br />
    4457      <p>
    45         <a href="/">Оди на почетната страница</a>
     58        <a href="/user">Оди на protected</a>
    4659      </p>
     60      <button onClick={handleLogout}>Одјави се</button>
    4761    </div>
     62    */
     63    <Navigate to="/user" />
    4864  ) : (
    4965    <div style={{ marginTop: "140px" }}>
  • reactapp/src/Pages/Professor.js

    r800779d r6eba109  
    1818
    1919  useEffect(() => {
    20     const url = `http://192.168.0.17:8080/public/professor/${params.professorId}`;
     20    const url = `http://192.168.0.18:8080/public/professor/${params.professorId}`;
    2121
    2222    const fetchData = async () => {
  • reactapp/src/api/axios.js

    r800779d r6eba109  
    22
    33export default axios.create({
    4   baseURL: "http://192.168.0.17:8080",
     4  baseURL: "http://192.168.0.18:8080",
    55});
Note: See TracChangeset for help on using the changeset viewer.