All files / src/apps/authentication utils.ts

30% Statements 3/10
0% Branches 0/5
0% Functions 0/4
30% Lines 3/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 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36                    1x             1x             1x                      
import { AxiosResponse } from "axios";
import store from "../../redux/store";
// resist the temptation to make this like the rest of the action creators
// because of the potential of a circular dependency(auth/utils->api->auth/api->auth/redux/actions->auth->utils)
import { LOGOUT_ACTION } from "./redux";
import { AppState } from "../../redux";
import { USER_TYPE } from "../../utils";
import { APIOrg, APIProfile } from "./types";
import { action } from "../../redux/utils";
 
export const redirectIfNotLoggedIn = (response: AxiosResponse) => {
  if (response.status === 401) {
    store.dispatch(action(LOGOUT_ACTION));
  }
  return response;
};
 
export const addAuthToken = (data: any, headers?: any) => {
  headers["Authorization"] = `Token ${
    (store.getState() as AppState).auth.token
  }`;
  return data;
};
 
export const canModifyContainer = (
  ownerType: string,
  owner: string,
  profile?: APIProfile,
  usersOrgs: APIOrg[] = []
) =>
  Boolean(
    ownerType === USER_TYPE
      ? profile?.username === owner
      : usersOrgs.map(org => org.id).includes(owner)
  );