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

implemented authentication in react

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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" }}>
Note: See TracChangeset for help on using the changeset viewer.