import React, {useContext, useState} from "react"; import {Link, useLocation, useNavigate} from "react-router-dom"; import styles from "./Login.module.css"; import illustration from "../../assets/illustration_img.png"; import Logo from "../../components/Logo/Logo.jsx"; import HttpService from "../../scripts/net/HttpService.js"; import {useAppContext} from "../../components/AppContext/AppContext.jsx"; import config from "../../scripts/net/netconfig.js"; const LoginPage = () => { const [formUsername, setFormUsername] = useState(""); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const navigate = useNavigate(); const location = useLocation(); const {setUsername, setIsAuthenticated} = useAppContext(); const {targetPath} = location.state || {targetPath: {pathname: "/"}}; const payload = { username: formUsername, password: password, }; const handleLogin = async () => { const httpService = new HttpService(); return httpService.post(config.auth.login, payload) }; const login = async (e) => { e.preventDefault(); handleLogin() .then(resp => { if (resp.token) { navigate(targetPath) localStorage.setItem("token", resp.token); setUsername(resp.username); setIsAuthenticated(true); console.log("ROLES",resp.roles) } else { setError("Invalid username or password."); } }).catch(reason => { console.error("Login failed", reason); setError("Login failed. Please try again.") }) // fetch("http://localhost:8080/api/auth/login", { // method: "POST", // headers: { // "Content-Type": "application/json", // }, // body: JSON.stringify(payload), // }) // .then((response) => { // if (!response.ok) { // throw new Error("Login failed: resp = " + response.statusText); // } // return response.json(); // }) // .then((data) => { // if (data.token) { // navigate(targetPath); // handleLogin(data); // } else { // setError("Invalid username or password."); // } // }) // .catch((error) => { // console.error("Login failed", error); // setError("Login failed. Please try again."); // }); }; return (
Don't have an account? Sign Up