[6eba109] | 1 | import React, { useRef, useState, useEffect, useContext } from "react";
|
---|
| 2 | import { Navigate } from "react-router-dom";
|
---|
| 3 | import AuthApi from "../api/AuthApi";
|
---|
[800779d] | 4 | import axios from "../api/axios";
|
---|
[6eba109] | 5 | import Cookies from "js-cookie";
|
---|
[cae16b5] | 6 | import { LoginButton, LoginInput } from "../Components/Styled/Login.style";
|
---|
[800779d] | 7 | const LOGIN_URL = "/login";
|
---|
| 8 |
|
---|
| 9 | const Login = () => {
|
---|
[6eba109] | 10 | const { auth, setAuth } = useContext(AuthApi);
|
---|
[800779d] | 11 | const userRef = useRef();
|
---|
| 12 | const errRef = useRef();
|
---|
| 13 |
|
---|
| 14 | const [username, setUsername] = useState("");
|
---|
| 15 | const [password, setPassword] = useState("");
|
---|
| 16 | const [errMsg, setErrMsg] = useState("");
|
---|
| 17 |
|
---|
| 18 | useEffect(() => {
|
---|
| 19 | userRef.current.focus();
|
---|
| 20 | }, []);
|
---|
| 21 |
|
---|
| 22 | const handleSubmit = async (e) => {
|
---|
| 23 | e.preventDefault();
|
---|
| 24 |
|
---|
| 25 | const response = await axios.post(
|
---|
| 26 | LOGIN_URL,
|
---|
| 27 | `username=${username}&password=${password}`,
|
---|
| 28 | {
|
---|
| 29 | headers: {
|
---|
| 30 | "content-type": "application/x-www-form-urlencoded",
|
---|
| 31 | withCredentials: true,
|
---|
| 32 | },
|
---|
| 33 | }
|
---|
| 34 | );
|
---|
[6eba109] | 35 | if (!response.request.responseURL.includes("error")) {
|
---|
| 36 | // ako NE redirektira na /login?error
|
---|
| 37 | Cookies.set("JSESSIONID", response.data.sessionId);
|
---|
| 38 | setAuth(true);
|
---|
| 39 | setErrMsg("");
|
---|
| 40 | } else {
|
---|
| 41 | setErrMsg("Погрешно корисиничко име и/или лозинка");
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[800779d] | 44 | setUsername("");
|
---|
| 45 | setPassword("");
|
---|
| 46 | };
|
---|
| 47 |
|
---|
[6eba109] | 48 | return auth ? (
|
---|
[702ca77] | 49 | <Navigate to="/user_dashboard" />
|
---|
[800779d] | 50 | ) : (
|
---|
[cae16b5] | 51 | <div
|
---|
| 52 | style={{
|
---|
| 53 | marginTop: "140px",
|
---|
| 54 | display: "flex",
|
---|
| 55 | alignItems: "center",
|
---|
| 56 | flexFlow: "column",
|
---|
| 57 | width: "fit-content",
|
---|
| 58 | margin: "auto",
|
---|
| 59 | border: "2px solid lightgrey",
|
---|
| 60 | padding: "25px",
|
---|
| 61 | boxShadow: "2px 2px 6px #aaaaaa",
|
---|
| 62 | marginBottom: "80px",
|
---|
| 63 | }}
|
---|
| 64 | >
|
---|
| 65 | <h2>Најава</h2>
|
---|
| 66 | <form
|
---|
| 67 | onSubmit={handleSubmit}
|
---|
| 68 | style={{
|
---|
| 69 | display: "flex",
|
---|
| 70 | flexDirection: "column",
|
---|
| 71 | alignItems: "center",
|
---|
| 72 | width: "400px",
|
---|
| 73 | }}
|
---|
| 74 | >
|
---|
| 75 | <LoginInput
|
---|
[800779d] | 76 | type="text"
|
---|
| 77 | id="username"
|
---|
| 78 | ref={userRef}
|
---|
| 79 | autoComplete="off"
|
---|
| 80 | onChange={(e) => setUsername(e.target.value)}
|
---|
| 81 | value={username}
|
---|
| 82 | required
|
---|
[cae16b5] | 83 | placeholder="Email"
|
---|
[800779d] | 84 | />
|
---|
[cae16b5] | 85 | <LoginInput
|
---|
[800779d] | 86 | type="password"
|
---|
| 87 | id="password"
|
---|
| 88 | onChange={(e) => setPassword(e.target.value)}
|
---|
| 89 | value={password}
|
---|
[cae16b5] | 90 | placeholder="Лозинка"
|
---|
[800779d] | 91 | required
|
---|
| 92 | />
|
---|
[cae16b5] | 93 | <LoginButton>Најави се</LoginButton>
|
---|
| 94 | <p
|
---|
| 95 | ref={errRef}
|
---|
| 96 | className={errMsg ? "errmsg" : "offscreen"}
|
---|
| 97 | style={{ color: "red", marginTop: "20px" }}
|
---|
| 98 | >
|
---|
| 99 | {errMsg}
|
---|
| 100 | </p>
|
---|
[800779d] | 101 | </form>
|
---|
[cae16b5] | 102 | <p style={{ marginTop: "20px" }}>
|
---|
| 103 | Немаш сметка? <a href="/registration">Регистрирај се</a>{" "}
|
---|
| 104 | </p>
|
---|
[800779d] | 105 | </div>
|
---|
| 106 | );
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | export default Login;
|
---|