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
 | 
			
		||||
  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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue