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"; | } from "../Types/goTypes"; | ||||||
| 
 | 
 | ||||||
| // This type of pattern should be hard to misuse
 | // This type of pattern should be hard to misuse
 | ||||||
| interface APIResponse<T> { | export interface APIResponse<T> { | ||||||
|   success: boolean; |   success: boolean; | ||||||
|   message?: string; |   message?: string; | ||||||
|   data?: T; |   data?: T; | ||||||
|  |  | ||||||
|  | @ -1,34 +1,54 @@ | ||||||
| import { NewUser } from "../Types/goTypes"; | 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; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -15,9 +15,10 @@ function LoginPage(props: { | ||||||
|    and if so, redirect to correct page */ |    and if so, redirect to correct page */ | ||||||
|   function handleSubmit(event: FormEvent<HTMLFormElement>): void { |   function handleSubmit(event: FormEvent<HTMLFormElement>): void { | ||||||
|     event.preventDefault(); |     event.preventDefault(); | ||||||
|     props.setAuthority((prevAuth) => { |     LoginCheck({ | ||||||
|       prevAuth = LoginCheck({ username: username, password: password }); |       username: username, | ||||||
|       return prevAuth; |       password: password, | ||||||
|  |       setAuthority: props.setAuthority, | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -14,6 +14,7 @@ function PMProjectMembers(): JSX.Element { | ||||||
|           onClick={(): void => { |           onClick={(): void => { | ||||||
|             return; |             return; | ||||||
|           }} |           }} | ||||||
|  |           type={"button"} | ||||||
|         /> |         /> | ||||||
|       </Link> |       </Link> | ||||||
|       <Link to="/PM-time-role"> |       <Link to="/PM-time-role"> | ||||||
|  | @ -22,6 +23,7 @@ function PMProjectMembers(): JSX.Element { | ||||||
|           onClick={(): void => { |           onClick={(): void => { | ||||||
|             return; |             return; | ||||||
|           }} |           }} | ||||||
|  |           type={"button"} | ||||||
|         /> |         /> | ||||||
|       </Link> |       </Link> | ||||||
|       <BackButton /> |       <BackButton /> | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Davenludd
						Davenludd