Now checks if user is in database with api.login and sets proper authority level based on name for now

This commit is contained in:
Peter KW 2024-03-17 19:16:57 +01:00
parent 141e5c8bb6
commit e7e79ced13

View file

@ -1,34 +1,54 @@
import { NewUser } from "../Types/Users"; 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 * Checks if user is in database with api.login and then sets proper authority level
const admin: NewUser = { * TODO: change so that it checks for user type (admin, user, pm) somehow instead
userName: "admin", **/
password: "123", function LoginCheck(props: {
}; username: string;
const pmanager: NewUser = { password: string;
userName: "pmanager", setAuthority: Dispatch<SetStateAction<number>>;
password: "123", }): number {
};
const user: NewUser = { const user: NewUser = {
userName: "user", username: props.username,
password: "123", password: props.password,
}; };
api
//TODO: Compare with db instead when finished .login(user)
if (props.username === admin.userName && props.password === admin.password) { .then((response: APIResponse<string>) => {
return 1; if (response.success) {
} else if ( if (response.data !== undefined) {
props.username === pmanager.userName && const token = response.data;
props.password === pmanager.password //TODO: change so that it checks for user type (admin, user, pm) instead
) { if (token !== "" && props.username === "admin") {
return 2; props.setAuthority((prevAuth) => {
} else if ( prevAuth = 1;
props.username === user.userName && return prevAuth;
props.password === user.password });
) { } else if (token !== "" && props.username === "pm") {
return 3; 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);
});
return 0; return 0;
} }