All files / events OnCheckSession.ts

83.33% Statements 35/42
82.61% Branches 38/46
100% Functions 2/2
83.33% Lines 35/42
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 108 1099x 9x   9x               9x   25x 25x 25x 5x     25x 25x       25x   25x 25x 25x 25x         25x           25x     25x 2x         2x       23x 1x         1x       22x 22x       4x 4x     18x     18x         2x         2x     16x                     16x         16x     9x  
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;
  }
 
  // Resetting password
  const codeDelivery = _.get(session, ['CodeDeliveryDetails', 'Destination']);
  if (
    challengeName === 'NEW_PASSWORD_REQUIRED' ||
    (!_.isNil(codeDelivery) && !_.isEmpty(codeDelivery))
  ) {
    result = { state: '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
    result = { state: 'Unauthenticated', error: new Error(session.message) };
    console.log(
      '*** Cognito OnCheckSession other code: "' + code + '" result:'
    );
    console.log(result);
 
    return result;
  }
 
  result = {
    state: isError ? 'AuthenticationError' : 'Unauthenticated',
    error: isError ? session : undefined,
  };
 
  return result;
};
 
export default OnCheckSession;