1 | import React, {useContext, useState} from "react";
|
---|
2 | import {Link, useLocation, useNavigate} from "react-router-dom";
|
---|
3 | import styles from "./Login.module.css";
|
---|
4 | import illustration from "../../assets/illustration_img.png";
|
---|
5 | import Logo from "../../components/Logo/Logo.jsx";
|
---|
6 | import HttpService from "../../scripts/net/HttpService.js";
|
---|
7 | import {useAppContext} from "../../components/AppContext/AppContext.jsx";
|
---|
8 | import config from "../../scripts/net/netconfig.js";
|
---|
9 |
|
---|
10 | const LoginPage = () => {
|
---|
11 | const [formUsername, setFormUsername] = useState("");
|
---|
12 | const [password, setPassword] = useState("");
|
---|
13 | const [error, setError] = useState(null);
|
---|
14 | const navigate = useNavigate();
|
---|
15 | const location = useLocation();
|
---|
16 |
|
---|
17 | const {setUsername, setIsAuthenticated} = useAppContext();
|
---|
18 |
|
---|
19 | const {targetPath} = location.state || {targetPath: {pathname: "/"}};
|
---|
20 |
|
---|
21 | const payload = {
|
---|
22 | username: formUsername,
|
---|
23 | password: password,
|
---|
24 | };
|
---|
25 |
|
---|
26 | const handleLogin = async () => {
|
---|
27 | const httpService = new HttpService();
|
---|
28 | return httpService.post(config.auth.login, payload)
|
---|
29 |
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | const login = async (e) => {
|
---|
34 | e.preventDefault();
|
---|
35 |
|
---|
36 | handleLogin()
|
---|
37 | .then(resp => {
|
---|
38 | if (resp.token) {
|
---|
39 | navigate(targetPath)
|
---|
40 | localStorage.setItem("token", resp.token);
|
---|
41 | setUsername(resp.username);
|
---|
42 | setIsAuthenticated(true);
|
---|
43 | console.log("ROLES",resp.roles)
|
---|
44 | } else {
|
---|
45 | setError("Invalid username or password.");
|
---|
46 | }
|
---|
47 | }).catch(reason => {
|
---|
48 | console.error("Login failed", reason);
|
---|
49 | setError("Login failed. Please try again.")
|
---|
50 | })
|
---|
51 |
|
---|
52 | // fetch("http://localhost:8080/api/auth/login", {
|
---|
53 | // method: "POST",
|
---|
54 | // headers: {
|
---|
55 | // "Content-Type": "application/json",
|
---|
56 | // },
|
---|
57 | // body: JSON.stringify(payload),
|
---|
58 | // })
|
---|
59 | // .then((response) => {
|
---|
60 | // if (!response.ok) {
|
---|
61 | // throw new Error("Login failed: resp = " + response.statusText);
|
---|
62 | // }
|
---|
63 | // return response.json();
|
---|
64 | // })
|
---|
65 | // .then((data) => {
|
---|
66 | // if (data.token) {
|
---|
67 | // navigate(targetPath);
|
---|
68 | // handleLogin(data);
|
---|
69 | // } else {
|
---|
70 | // setError("Invalid username or password.");
|
---|
71 | // }
|
---|
72 | // })
|
---|
73 | // .catch((error) => {
|
---|
74 | // console.error("Login failed", error);
|
---|
75 | // setError("Login failed. Please try again.");
|
---|
76 | // });
|
---|
77 | };
|
---|
78 |
|
---|
79 | return (
|
---|
80 | <div className={styles.wrapper}>
|
---|
81 | <Logo></Logo>
|
---|
82 | <div className={styles.illustration}>
|
---|
83 | <img src={illustration} alt="illustration"/>
|
---|
84 | </div>
|
---|
85 | <div className={styles.form}>
|
---|
86 | <div className={styles.heading}>LOGIN</div>
|
---|
87 | <form onSubmit={login}>
|
---|
88 | <div>
|
---|
89 | <label htmlFor="username">Username</label>
|
---|
90 | <input
|
---|
91 | type="text"
|
---|
92 | id="name"
|
---|
93 | placeholder="Enter your username"
|
---|
94 | onChange={(e) => setFormUsername(e.target.value)}
|
---|
95 | value={formUsername}
|
---|
96 | required
|
---|
97 | />
|
---|
98 | </div>
|
---|
99 | <div>
|
---|
100 | <label htmlFor="password">Password</label>
|
---|
101 | <input
|
---|
102 | type="password"
|
---|
103 | id="password"
|
---|
104 | placeholder="Enter your password"
|
---|
105 | onChange={(e) => setPassword(e.target.value)}
|
---|
106 | value={password}
|
---|
107 | required
|
---|
108 | />
|
---|
109 | </div>
|
---|
110 | {error && <p className={styles.error}>{error}</p>}
|
---|
111 | <button type="submit">Submit</button>
|
---|
112 | </form>
|
---|
113 | <p>
|
---|
114 | Don't have an account? <Link to="/Signup"> Sign Up </Link>
|
---|
115 | </p>
|
---|
116 | </div>
|
---|
117 | </div>
|
---|
118 | );
|
---|
119 | };
|
---|
120 |
|
---|
121 | export default LoginPage;
|
---|