All files OnCheckSession.ts

85% Statements 34/40
82.22% Branches 37/45
100% Functions 2/2
85% Lines 34/40
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 105 106 107 1087x 7x   7x               7x   18x 18x 18x 2x     18x 18x       18x   18x 18x 18x 18x         18x           18x     18x 2x         2x       16x 1x         1x     15x 1x         1x     14x     14x         2x         2x     12x                     12x         12x     7x  
import { CognitoUser } from 'amazon-cognito-identity-js';
import _ from 'lodash';
import { IAuthenticationResult } from '../types';
import { Auth } from 'aws-amplify';
 
/**
 * Check user authentication state
 * @param cognitoAuthResult optional result of authentication operation.
 * Else `Auth.currentSession()`
 * @returns See {@link IAuthenticationResult}
 */
export const OnCheckSession = async (
  cognitoAuthResult?: CognitoUser | any | undefined
): Promise<IAuthenticationResult> => {
  let session = cognitoAuthResult;
  if (_.isNil(session)) {
    session = await Auth.currentSession();
  }
 
  let user = session instanceof CognitoUser ? session : null;
  Iif (_.get(session, 'user') && session.user instanceof CognitoUser) {
    user = session.user;
  }
 
  const isError: boolean = session instanceof Error;
 
  Eif (__DEV__) {
    console.log('**** OnCheckSession ****');
    console.log('isError? ' + isError);
    console.log(session);
  }
 
  let result: IAuthenticationResult;
 
  Iif (_.get(user, 'username') && _.get(session, 'userConfirmed') !== false) {
    result = { state: 'Authenticated', user: session as CognitoUser };
 
    return result;
  }
 
  const challengeName: string = _.get(session, 'challengeName') || '';
 
  // MFA code required
  if (challengeName === 'SMS_MFA' || challengeName === 'SOFTWARE_TOKEN_MFA') {
    result = {
      state: 'ConfirmLoginMFAWaiting',
      error: new Error(_.get(session, 'message') || 'Please enter MFA code'),
    };
 
    return result;
  }
 
  // Need to setup MFA
  if (challengeName === 'MFA_SETUP') {
    result = {
      state: 'MFA_SETUP',
      error: new Error(_.get(session, 'message') || 'Must setup MFA'),
    };
 
    return result;
  }
 
  if (challengeName === 'NEW_PASSWORD_REQUIRED') {
    result = {
      state: 'NEW_PASSWORD_REQUIRED',
      error: new Error(_.get(session, 'message') || 'New password required'),
    };
 
    return result;
  }
 
  const code = _.get(session, 'code');
 
  // Not confirmed
  if (
    code === 'UserNotConfirmedException' ||
    code === 'CodeMismatchException' ||
    _.get(session, 'userConfirmed') === false
  ) {
    result = {
      state: 'ConfirmAccountCodeWaiting',
      error: new Error(_.get(session, 'message') || 'User not confirmed'),
    };
 
    return result;
  }
 
  Iif (!_.isNil(code) && _.get(session, 'message')) {
    // Other codes
    console.log('*** Cognito OnCheckSession code: "' + code + '" ***');
    result = {
      state: 'Unauthenticated',
      error: new Error(session.message),
    };
 
    return result;
  }
 
  result = {
    state: isError ? 'AuthenticationError' : 'Unauthenticated',
    error: isError ? session : undefined,
  };
 
  return result;
};
 
export default OnCheckSession;