diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx index dc29604..14672bc 100644 --- a/frontend/src/pages/LoginPage.jsx +++ b/frontend/src/pages/LoginPage.jsx @@ -1,29 +1,39 @@ import React, { useState } from "react"; -import { Button, Col, Container, Form, Row } from "react-bootstrap"; +import { Button, Col, Container, Form, Row, Alert} from "react-bootstrap"; import MyNavbar from "../components/MyNavbar"; import { makeRequest } from "../utils.ts"; const LoginPage = () => { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); + const [error, setError] = useState(null); - const sendLoginRequest = async () => { - // await makeRequest({ - // url: "http://localhost:5000/login", - // method: "POST", - // body: { username, password }, - // }) - // .then((resp) => resp.json()) - // .then((data) => { - // console.log(data); - // }); + const sendLoginRequest = async (e) => { + e.preventDefault(); + await makeRequest({ + url: "http://localhost:5000/login", + method: "POST", + body: { username, password }, + }) + .then((resp) => resp.json()) + .then((data) => { + if (data.error) { + setError(data); + return; + } + console.log(data); + window.location.href = "/"; + }); }; return ( + { error && ( + ERROR! {error.message} + )} -
+ Username diff --git a/frontend/src/utils.ts b/frontend/src/utils.ts new file mode 100644 index 0000000..c7b116f --- /dev/null +++ b/frontend/src/utils.ts @@ -0,0 +1,11 @@ +const makeRequest = ({ url, method, body }): Promise => { + return fetch(url, { + method: method, + credentials: "include", + body: JSON.stringify(body), + headers: { "content-type": "application/json" }, + mode: "cors", + }); +}; + +export { makeRequest };