All files / react-app/src/utils user.ts

100% Statements 12/12
100% Branches 9/9
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21          73x   916x 880x   73x 863x 863x 863x 860x   3x 3x      
import { STATE_CODES, StateCode } from "shared-types/states";
import { type CognitoUserAttributes } from "shared-types/user";
import { isCmsUser, isStateUser } from "shared-utils";
import { getUser } from "@/api";
 
export const getUserStateCodes = (user: CognitoUserAttributes | null | undefined): StateCode[] => {
  // We always need a user, and state users always need a custom:state value
  if (!user || (isStateUser(user) && user["custom:state"] === undefined)) return [];
  return isCmsUser(user) ? [...STATE_CODES] : (user["custom:state"]!.split(",") as StateCode[]);
};
export const isAuthorizedState = async (id: string) => {
  try {
    const user = await getUser();
    if (!user.user) throw Error("No cognito attributes found.");
    return getUserStateCodes(user.user).includes(id.substring(0, 2) as StateCode);
  } catch (e) {
    console.error(e);
    return false;
  }
};