diff --git a/src/pages/Login/Login.tsx b/src/pages/Login/Login.tsx index d9f5c40d823a8d2dae70f4a3d2d490ffb3dc0a90..5e137f3a963fc61f44b8d4a882dc827b1d6bf5a2 100644 --- a/src/pages/Login/Login.tsx +++ b/src/pages/Login/Login.tsx @@ -9,6 +9,7 @@ import { REST_BASE_URL } from "../../constant/constants"; import FormGroup from "@/components/AuthenticationForm/FormGroup"; import FormButton from "@/components/AuthenticationForm/FormButton"; +import { login } from "@/utils/login"; const Login = () => { const [username, setUsername] = useState<string>(""); @@ -30,24 +31,11 @@ const Login = () => { const onLogin = async (e: React.SyntheticEvent) => { e.preventDefault(); - // Build JSON data to be sent - const requestBody = { - username, - password, - }; + const response = login(username, password); - // Send POST request - const response = await fetch(`${REST_BASE_URL}/user/token`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(requestBody), - }); + const data = await response; - const data = await response.json(); - - if (!response.ok) { + if (!data.ok) { toast.error(data.message, { position: "top-center", autoClose: 1500, @@ -59,7 +47,6 @@ const Login = () => { theme: "light", }); } else { - localStorage.setItem("token", `Bearer ${data.token}`); navigate("/"); } };