1 | import React, { useRef, useState, useEffect, useContext } from "react";
|
---|
2 | import { Navigate } from "react-router-dom";
|
---|
3 | import AuthApi from "../api/AuthApi";
|
---|
4 | import axios from "../api/axios";
|
---|
5 | import Cookies from "js-cookie";
|
---|
6 | import { LoginButton, LoginInput } from "../Components/Styled/Login.style";
|
---|
7 | const LOGIN_URL = "/login";
|
---|
8 |
|
---|
9 | const Login = () => {
|
---|
10 | const { auth, setAuth } = useContext(AuthApi);
|
---|
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 | );
|
---|
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 |
|
---|
44 | setUsername("");
|
---|
45 | setPassword("");
|
---|
46 | };
|
---|
47 |
|
---|
48 | return auth ? (
|
---|
49 | <Navigate to="/user_dashboard" />
|
---|
50 | ) : (
|
---|
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
|
---|
76 | type="text"
|
---|
77 | id="username"
|
---|
78 | ref={userRef}
|
---|
79 | autoComplete="off"
|
---|
80 | onChange={(e) => setUsername(e.target.value)}
|
---|
81 | value={username}
|
---|
82 | required
|
---|
83 | placeholder="Email"
|
---|
84 | />
|
---|
85 | <LoginInput
|
---|
86 | type="password"
|
---|
87 | id="password"
|
---|
88 | onChange={(e) => setPassword(e.target.value)}
|
---|
89 | value={password}
|
---|
90 | placeholder="Лозинка"
|
---|
91 | required
|
---|
92 | />
|
---|
93 | <LoginButton>Најави се</LoginButton>
|
---|
94 | <p
|
---|
95 | ref={errRef}
|
---|
96 | className={errMsg ? "errmsg" : "offscreen"}
|
---|
97 | style={{ color: "red", marginTop: "20px" }}
|
---|
98 | >
|
---|
99 | {errMsg}
|
---|
100 | </p>
|
---|
101 | </form>
|
---|
102 | <p style={{ marginTop: "20px" }}>
|
---|
103 | Немаш сметка? <a href="/registration">Регистрирај се</a>{" "}
|
---|
104 | </p>
|
---|
105 | </div>
|
---|
106 | );
|
---|
107 | };
|
---|
108 |
|
---|
109 | export default Login;
|
---|