All files / web/modules/auth reducer.ts

38.89% Statements 14/36
0% Branches 0/5
0% Functions 0/12
46.67% Lines 14/30

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1052x 2x     2x             2x                                                                         2x                                     2x     2x     2x     2x   2x   2x   2x             2x                         2x    
import * as moment from 'moment';
import { Groups } from '../../../shared';
import { IState } from '../../types';
import { Action } from './actions';
import {
  AUTH_SET_AUTH_DATA,
  AUTH_SET_AUTH_STATE,
  AUTH_SET_IS_LOADING,
  AUTH_STATE,
  AUTH_STATE_DEFAULT,
} from './constants';
const get = require('lodash/get');
 
export interface IAuthData {
  authenticationFlowType?: string;
  challengeName?: string;
  client?: {
    endpoint: string;
    userAgent: string;
  };
  signInUserSession?: {
    accessToken: {
      jwtToken: string;
    };
    idToken: {
      payload: any;
    };
  };
  username?: string;
  attributes?: {
    sub: string;
  };
}
 
export interface IAuthReducer {
  authData: IAuthData;
  authState: AUTH_STATE;
  totp?: string;
  isLoading: boolean;
}
 
const initialState: IAuthReducer = {
  authData: {},
  authState: AUTH_STATE_DEFAULT,
  totp: undefined,
  isLoading: true,
};
 
export default (state = initialState, action: Action) => {
  switch (action.type) {
    case AUTH_SET_AUTH_STATE: {
      const { authState } = action;
      return { ...state, authState };
    }
    case AUTH_SET_AUTH_DATA: {
      const { authData } = action;
      return { ...state, authData };
    }
    case AUTH_SET_IS_LOADING: {
      const { isLoading } = action;
      return { ...state, isLoading };
    }
    default:
      return state;
  }
};
 
export const selectToken = (state: IState) =>
  get(state, 'authReducer.authData.signInUserSession.accessToken.jwtToken');
 
export const selectExpirationDate = (state: IState) =>
  get(state, 'authReducer.authData.attributes.custom:expirationDate');
 
export const selectUsername = (state: IState) =>
  state.authReducer.authData.username;
 
export const selectAuthData = (state: IState) => state.authReducer.authData;
 
export const selectAuthState = (state: IState) => state.authReducer.authState;
 
export const selectIsLoading = (state: IState) => state.authReducer.isLoading;
 
export const selectGroups = (state: IState) =>
  get(
    state.authReducer.authData,
    'signInUserSession.idToken.payload.cognito:groups',
    [],
  );
 
export const selectHasPermission = (state: IState) => (
  groups: string[],
): boolean => {
  const hasGroup =
    groups.filter(n => selectGroups(state).includes(n)).length > 0;
 
  if (selectGroups(state).includes(Groups.registered)) {
    const isExpired = moment().isSameOrAfter(selectExpirationDate(state));
    return !isExpired && hasGroup;
  }
  return hasGroup;
};
 
export const selectUserSub = (state: IState) =>
  get(state, 'authReducer.authData.attributes.sub');