diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index d87594c..cfd5b61 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -7,7 +7,7 @@ import { } from "../Types/goTypes"; // This type of pattern should be hard to misuse -interface APIResponse { +export interface APIResponse { success: boolean; message?: string; data?: T; diff --git a/frontend/src/Components/LoginCheck.tsx b/frontend/src/Components/LoginCheck.tsx index af45d0f..3658cbf 100644 --- a/frontend/src/Components/LoginCheck.tsx +++ b/frontend/src/Components/LoginCheck.tsx @@ -1,34 +1,54 @@ import { NewUser } from "../Types/goTypes"; +import { api, APIResponse } from "../API/API"; +import { Dispatch, SetStateAction } from "react"; -function LoginCheck(props: { username: string; password: string }): number { - //Example users for testing without backend, remove when using backend - const admin: NewUser = { - username: "admin", - password: "123", - }; - const pmanager: NewUser = { - username: "pmanager", - password: "123", - }; +/* + * Checks if user is in database with api.login and then sets proper authority level + * TODO: change so that it checks for user type (admin, user, pm) somehow instead + **/ +function LoginCheck(props: { + username: string; + password: string; + setAuthority: Dispatch>; +}): number { const user: NewUser = { - username: "user", - password: "123", + username: props.username, + password: props.password, }; + api + .login(user) + .then((response: APIResponse) => { + if (response.success) { + if (response.data !== undefined) { + const token = response.data; + //TODO: change so that it checks for user type (admin, user, pm) instead + if (token !== "" && props.username === "admin") { + props.setAuthority((prevAuth) => { + prevAuth = 1; + return prevAuth; + }); + } else if (token !== "" && props.username === "pm") { + props.setAuthority((prevAuth) => { + prevAuth = 2; + return prevAuth; + }); + } else if (token !== "" && props.username === "user") { + props.setAuthority((prevAuth) => { + prevAuth = 3; + return prevAuth; + }); + } + } else { + console.error("Token was undefined"); + } + } else { + console.error("Token could not be fetched"); + } + }) + .catch((error) => { + console.error("An error occurred during login:", error); + }); - //TODO: Compare with db instead when finished - if (props.username === admin.username && props.password === admin.password) { - return 1; - } else if ( - props.username === pmanager.username && - props.password === pmanager.password - ) { - return 2; - } else if ( - props.username === user.username && - props.password === user.password - ) { - return 3; - } return 0; } diff --git a/frontend/src/Pages/LoginPage.tsx b/frontend/src/Pages/LoginPage.tsx index f58f3e0..1ff8c9c 100644 --- a/frontend/src/Pages/LoginPage.tsx +++ b/frontend/src/Pages/LoginPage.tsx @@ -15,9 +15,10 @@ function LoginPage(props: { and if so, redirect to correct page */ function handleSubmit(event: FormEvent): void { event.preventDefault(); - props.setAuthority((prevAuth) => { - prevAuth = LoginCheck({ username: username, password: password }); - return prevAuth; + LoginCheck({ + username: username, + password: password, + setAuthority: props.setAuthority, }); } diff --git a/frontend/src/Pages/ProjectManagerPages/PMProjectMembers.tsx b/frontend/src/Pages/ProjectManagerPages/PMProjectMembers.tsx index 2af1145..bbafd6a 100644 --- a/frontend/src/Pages/ProjectManagerPages/PMProjectMembers.tsx +++ b/frontend/src/Pages/ProjectManagerPages/PMProjectMembers.tsx @@ -14,6 +14,7 @@ function PMProjectMembers(): JSX.Element { onClick={(): void => { return; }} + type={"button"} /> @@ -22,6 +23,7 @@ function PMProjectMembers(): JSX.Element { onClick={(): void => { return; }} + type={"button"} />