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:
parent
141e5c8bb6
commit
e7e79ced13
1 changed files with 47 additions and 27 deletions
|
@ -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
|
||||||
|
.login(user)
|
||||||
|
.then((response: APIResponse<string>) => {
|
||||||
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue