Merge branch 'frontend' into gruppDM
This commit is contained in:
commit
ead1482e50
4 changed files with 53 additions and 30 deletions
|
@ -7,7 +7,7 @@ import {
|
|||
} from "../Types/goTypes";
|
||||
|
||||
// This type of pattern should be hard to misuse
|
||||
interface APIResponse<T> {
|
||||
export interface APIResponse<T> {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
data?: T;
|
||||
|
|
|
@ -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<SetStateAction<number>>;
|
||||
}): number {
|
||||
const user: NewUser = {
|
||||
username: "user",
|
||||
password: "123",
|
||||
username: props.username,
|
||||
password: props.password,
|
||||
};
|
||||
|
||||
//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;
|
||||
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);
|
||||
});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -15,9 +15,10 @@ function LoginPage(props: {
|
|||
and if so, redirect to correct page */
|
||||
function handleSubmit(event: FormEvent<HTMLFormElement>): void {
|
||||
event.preventDefault();
|
||||
props.setAuthority((prevAuth) => {
|
||||
prevAuth = LoginCheck({ username: username, password: password });
|
||||
return prevAuth;
|
||||
LoginCheck({
|
||||
username: username,
|
||||
password: password,
|
||||
setAuthority: props.setAuthority,
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ function PMProjectMembers(): JSX.Element {
|
|||
onClick={(): void => {
|
||||
return;
|
||||
}}
|
||||
type={"button"}
|
||||
/>
|
||||
</Link>
|
||||
<Link to="/PM-time-role">
|
||||
|
@ -22,6 +23,7 @@ function PMProjectMembers(): JSX.Element {
|
|||
onClick={(): void => {
|
||||
return;
|
||||
}}
|
||||
type={"button"}
|
||||
/>
|
||||
</Link>
|
||||
<BackButton />
|
||||
|
|
Loading…
Reference in a new issue