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";
import { LoginButton, LoginInput } from "../Components/Styled/Login.style";
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(() => {
if (!auth) 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
var in30Minutes = 1 / 48;
Cookies.set("JSESSIONID", response.data.sessionId, {
expires: in30Minutes,
});
setAuth(true);
setErrMsg("");
} else {
setErrMsg("Погрешно корисиничко име и/или лозинка");
}
setUsername("");
setPassword("");
};
return auth ? (
Немаш сметка? Регистрирај се{" "}