import { NewUser } from "../Types/goTypes"; import { api, APIResponse } from "../API/API"; import { Dispatch, SetStateAction } from "react"; /* * 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>; }): void { if (props.username === "" || props.password === "") { alert("Please enter username and password to login"); return; } const user: NewUser = { username: props.username, password: props.password, }; localStorage.clear(); api .login(user) .then((response: APIResponse) => { if (response.success) { if (response.data !== undefined) { const token = response.data; localStorage.setItem("accessToken", token); localStorage.setItem("username", props.username); //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.setAuthority((prevAuth) => { prevAuth = 2; return prevAuth; }); } } else { console.error("Token was undefined"); } } else { if (response.data === "500") { console.error(response.message); alert("No connection/Error"); } else { console.error( "Token could not be fetched/No such user" + response.message, ); alert("Incorrect login information"); } } }) .catch((error) => { console.error("An error occurred during login:", error); }); } export default LoginCheck;