import React, { useRef, useState, useEffect, useContext } from "react"; import { Navigate } from "react-router-dom"; import AuthApi from "../api/AuthApi"; import axios from "../api/axios"; import Cookies from "js-cookie"; const LOGIN_URL = "/login"; const Login = () => { const { auth, setAuth } = useContext(AuthApi); const userRef = useRef(); const errRef = useRef(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [errMsg, setErrMsg] = useState(""); useEffect(() => { userRef.current.focus(); }, []); const handleSubmit = async (e) => { e.preventDefault(); const response = await axios.post( LOGIN_URL, `username=${username}&password=${password}`, { headers: { "content-type": "application/x-www-form-urlencoded", withCredentials: true, }, } ); if (!response.request.responseURL.includes("error")) { // ako NE redirektira na /login?error Cookies.set("JSESSIONID", response.data.sessionId); setAuth(true); setErrMsg(""); } else { setErrMsg("Погрешно корисиничко име и/или лозинка"); } setUsername(""); setPassword(""); }; return auth ? ( ) : (

{errMsg}

Најава

setUsername(e.target.value)} value={username} required /> setPassword(e.target.value)} value={password} required />
); }; export default Login;