source: imaps-frontend/src/pages/Login/Login.jsx@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[0c6b92a]1import React, {useContext, useState} from "react";
2import {Link, useLocation, useNavigate} from "react-router-dom";
[d565449]3import styles from "./Login.module.css";
4import illustration from "../../assets/illustration_img.png";
[0c6b92a]5import Logo from "../../components/Logo/Logo.jsx";
6import HttpService from "../../scripts/net/HttpService.js";
7import {useAppContext} from "../../components/AppContext/AppContext.jsx";
8import config from "../../scripts/net/netconfig.js";
[d565449]9
[0c6b92a]10const 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();
[d565449]16
[0c6b92a]17 const {setUsername, setIsAuthenticated} = useAppContext();
[d565449]18
[0c6b92a]19 const {targetPath} = location.state || {targetPath: {pathname: "/"}};
[d565449]20
[0c6b92a]21 const payload = {
22 username: formUsername,
23 password: password,
24 };
[d565449]25
[0c6b92a]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 );
[d565449]119};
120
121export default LoginPage;
Note: See TracBrowser for help on using the repository browser.