| 1 | import { useState } from "react";
|
|---|
| 2 | import axios from "axios";
|
|---|
| 3 | import { useNavigate } from "react-router-dom";
|
|---|
| 4 | import LoginStyles from "./LoginStyles.module.css";
|
|---|
| 5 |
|
|---|
| 6 | import logo from "../../assets/logo.png";
|
|---|
| 7 |
|
|---|
| 8 | function Login() {
|
|---|
| 9 | const [email, setEmail] = useState("");
|
|---|
| 10 | const [password, setPassword] = useState("");
|
|---|
| 11 | const [error, setError] = useState("");
|
|---|
| 12 | const [isLoading, setIsLoading] = useState(false);
|
|---|
| 13 |
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 |
|
|---|
| 16 | const handleSubmit = async (e) => {
|
|---|
| 17 | e.preventDefault();
|
|---|
| 18 | setError("");
|
|---|
| 19 | setIsLoading(true);
|
|---|
| 20 |
|
|---|
| 21 | try {
|
|---|
| 22 | const response = await axios.post(
|
|---|
| 23 | "http://localhost:8080/api/auth/login",
|
|---|
| 24 | { email, password },
|
|---|
| 25 | { withCredentials: true }
|
|---|
| 26 | );
|
|---|
| 27 |
|
|---|
| 28 | if (response.status === 200) {
|
|---|
| 29 | const user = response.data;
|
|---|
| 30 |
|
|---|
| 31 | sessionStorage.setItem("user", JSON.stringify(user));
|
|---|
| 32 | console.log("ssssssssssss");
|
|---|
| 33 | console.log(user);
|
|---|
| 34 | navigate("/dashboard");
|
|---|
| 35 | }
|
|---|
| 36 | } catch (err) {
|
|---|
| 37 | setError("Грешен емаил или лозинка. Обидете се повторно.");
|
|---|
| 38 | console.error("Login error:", err);
|
|---|
| 39 | } finally {
|
|---|
| 40 | setIsLoading(false);
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | return (
|
|---|
| 45 | <div
|
|---|
| 46 | className="min-vh-100 d-flex align-items-center justify-content-center"
|
|---|
| 47 | style={{ backgroundColor: "#5CCCD2" }}
|
|---|
| 48 | >
|
|---|
| 49 | <div className="card shadow-lg w-100" style={{ maxWidth: "900px" }}>
|
|---|
| 50 | <div className="row g-0">
|
|---|
| 51 | <div
|
|---|
| 52 | className="col-md-6 d-flex flex-column align-items-center justify-content-center p-5 text-black rounded-start"
|
|---|
| 53 | style={{ backgroundColor: "#f5f5dc" }}
|
|---|
| 54 | >
|
|---|
| 55 | <div className="col mb-5">
|
|---|
| 56 | <img src={logo} className="img-fluid h-100 rounded" />
|
|---|
| 57 | </div>
|
|---|
| 58 | <h1 className="col fs-3 fw-bold text-center">
|
|---|
| 59 | Добредојдовте на web3 Е-Дневник
|
|---|
| 60 | </h1>
|
|---|
| 61 | </div>
|
|---|
| 62 |
|
|---|
| 63 | <div className="col-md-6 p-5 align-items-center">
|
|---|
| 64 | <h2 className="fs-5 fw-semibold mb-4 mt-1">
|
|---|
| 65 | Најавете се за да продолжите кон системот
|
|---|
| 66 | </h2>
|
|---|
| 67 |
|
|---|
| 68 | {error && <div className="alert alert-danger">{error}</div>}
|
|---|
| 69 |
|
|---|
| 70 | <form onSubmit={handleSubmit}>
|
|---|
| 71 | <div className="mb-1">
|
|---|
| 72 | <label htmlFor="email" className="form-label">
|
|---|
| 73 | Вашата е-пошта
|
|---|
| 74 | </label>
|
|---|
| 75 | <input
|
|---|
| 76 | id="email"
|
|---|
| 77 | type="email"
|
|---|
| 78 | className="form-control"
|
|---|
| 79 | value={email}
|
|---|
| 80 | name="email"
|
|---|
| 81 | onChange={(e) => setEmail(e.target.value)}
|
|---|
| 82 | placeholder="Е-пошта"
|
|---|
| 83 | required
|
|---|
| 84 | />
|
|---|
| 85 | </div>
|
|---|
| 86 |
|
|---|
| 87 | <div className="mb-3">
|
|---|
| 88 | <label htmlFor="password" className="form-label">
|
|---|
| 89 | Вашата лозинка
|
|---|
| 90 | </label>
|
|---|
| 91 | <input
|
|---|
| 92 | id="password"
|
|---|
| 93 | type="password"
|
|---|
| 94 | className="form-control"
|
|---|
| 95 | name="password"
|
|---|
| 96 | value={password}
|
|---|
| 97 | onChange={(e) => setPassword(e.target.value)}
|
|---|
| 98 | placeholder="Лозинка"
|
|---|
| 99 | required
|
|---|
| 100 | />
|
|---|
| 101 | </div>
|
|---|
| 102 |
|
|---|
| 103 | <button
|
|---|
| 104 | type="submit"
|
|---|
| 105 | className="btn w-100 text-white mt-2"
|
|---|
| 106 | style={{ backgroundColor: "#147176" }}
|
|---|
| 107 | disabled={isLoading}
|
|---|
| 108 | >
|
|---|
| 109 | {isLoading ? "Почекајте..." : "Најави се"}
|
|---|
| 110 | </button>
|
|---|
| 111 | </form>
|
|---|
| 112 | </div>
|
|---|
| 113 | </div>
|
|---|
| 114 | </div>
|
|---|
| 115 | </div>
|
|---|
| 116 | );
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | export default Login;
|
|---|