@@ -2,7 +2,12 @@ import { Auth } from 'aws-amplify';
22import get from 'lodash/get' ;
33import isEmpty from 'lodash/isEmpty' ;
44import { createMachine , sendUpdate } from 'xstate' ;
5- import { AuthChallengeNames , AuthEvent , SignInContext } from '../../../types' ;
5+ import {
6+ AuthChallengeName ,
7+ AuthEvent ,
8+ CognitoUserAmplify ,
9+ SignInContext ,
10+ } from '../../../types' ;
611import { runValidators } from '../../../validators' ;
712import {
813 clearAttributeToVerify ,
@@ -34,6 +39,22 @@ export type SignInMachineOptions = {
3439 services ?: Partial < typeof defaultServices > ;
3540} ;
3641
42+ const MFA_CHALLENGE_NAMES : AuthChallengeName [ ] = [
43+ 'SMS_MFA' ,
44+ 'SOFTWARE_TOKEN_MFA' ,
45+ ] ;
46+
47+ const getChallengeName = ( event : AuthEvent ) : AuthChallengeName =>
48+ get ( event , 'data.challengeName' ) ;
49+
50+ const isExpectedChallengeName = (
51+ challengeName : AuthChallengeName ,
52+ expectedChallengeName : AuthChallengeName
53+ ) => challengeName === expectedChallengeName ;
54+
55+ const isMfaChallengeName = ( challengeName : AuthChallengeName ) =>
56+ MFA_CHALLENGE_NAMES . includes ( challengeName ) ;
57+
3758export function signInActor ( { services } : SignInMachineOptions ) {
3859 return createMachine < SignInContext , AuthEvent > (
3960 {
@@ -461,13 +482,7 @@ export function signInActor({ services }: SignInMachineOptions) {
461482 } ,
462483 guards : {
463484 shouldConfirmSignIn : ( _ , event ) : boolean => {
464- const challengeName = get ( event , 'data.challengeName' ) ;
465- const validChallengeNames = [
466- AuthChallengeNames . SMS_MFA ,
467- AuthChallengeNames . SOFTWARE_TOKEN_MFA ,
468- ] ;
469-
470- return validChallengeNames . includes ( challengeName ) ;
485+ return isMfaChallengeName ( getChallengeName ( event ) ) ;
471486 } ,
472487 shouldAutoSignIn : ( context ) => {
473488 return context ?. intent === 'autoSignIn' ;
@@ -479,14 +494,13 @@ export function signInActor({ services }: SignInMachineOptions) {
479494 return event . data . code === 'PasswordResetRequiredException' ;
480495 } ,
481496 shouldSetupTOTP : ( _ , event ) : boolean => {
482- const challengeName = get ( event , 'data.challengeName' ) ;
483-
484- return challengeName === AuthChallengeNames . MFA_SETUP ;
497+ return isExpectedChallengeName ( getChallengeName ( event ) , 'MFA_SETUP' ) ;
485498 } ,
486499 shouldForceChangePassword : ( _ , event ) : boolean => {
487- const challengeName = get ( event , 'data.challengeName' ) ;
488-
489- return challengeName === AuthChallengeNames . NEW_PASSWORD_REQUIRED ;
500+ return isExpectedChallengeName (
501+ getChallengeName ( event ) ,
502+ 'NEW_PASSWORD_REQUIRED'
503+ ) ;
490504 } ,
491505 shouldRequestVerification : ( _ , event ) : boolean => {
492506 const { unverified, verified } = event . data ;
@@ -511,22 +525,18 @@ export function signInActor({ services }: SignInMachineOptions) {
511525 password,
512526 } ) ;
513527 } ,
514- async confirmSignIn ( context , event ) {
528+ async confirmSignIn ( context ) {
515529 const { challengeName, user } = context ;
516530 const { confirmation_code : code } = context . formValues ;
517531
518- let mfaType ;
519- if (
520- challengeName === AuthChallengeNames . SMS_MFA ||
521- challengeName === AuthChallengeNames . SOFTWARE_TOKEN_MFA
522- ) {
523- mfaType = challengeName ;
524- }
532+ const mfaType = isMfaChallengeName ( challengeName )
533+ ? challengeName
534+ : undefined ;
525535
526536 await services . handleConfirmSignIn ( { user, code, mfaType } ) ;
527537 return await Auth . currentAuthenticatedUser ( ) ;
528538 } ,
529- async forceNewPassword ( context , event ) {
539+ async forceNewPassword ( context ) {
530540 const { user, formValues } = context ;
531541 let {
532542 password,
@@ -545,7 +555,7 @@ export function signInActor({ services }: SignInMachineOptions) {
545555
546556 try {
547557 // complete forceNewPassword flow and get updated CognitoUser
548- const newUser = await Auth . completeNewPassword (
558+ const newUser : CognitoUserAmplify = await Auth . completeNewPassword (
549559 user ,
550560 password ,
551561 rest
@@ -569,9 +579,9 @@ export function signInActor({ services }: SignInMachineOptions) {
569579 return Promise . reject ( err ) ;
570580 }
571581 } ,
572- async verifyTotpToken ( context , event ) {
573- const { user } = context ;
574- const { confirmation_code } = context . formValues ;
582+ async verifyTotpToken ( context ) {
583+ const { formValues , user } = context ;
584+ const { confirmation_code } = formValues ;
575585
576586 return Auth . verifyTotpToken ( user , confirmation_code ) ;
577587 } ,
@@ -580,21 +590,21 @@ export function signInActor({ services }: SignInMachineOptions) {
580590
581591 return await Auth . federatedSignIn ( { provider } ) ;
582592 } ,
583- async checkVerifiedContact ( context , event ) {
593+ async checkVerifiedContact ( context ) {
584594 const { user } = context ;
585595 const result = await Auth . verifiedContact ( user ) ;
586596
587597 return result ;
588598 } ,
589- async verifyUser ( context , event ) {
599+ async verifyUser ( context ) {
590600 const { unverifiedAttr } = context . formValues ;
591601 const result = await Auth . verifyCurrentUserAttribute ( unverifiedAttr ) ;
592602
593603 context . attributeToVerify = unverifiedAttr ;
594604
595605 return result ;
596606 } ,
597- async confirmVerifyUser ( context , event ) {
607+ async confirmVerifyUser ( context ) {
598608 const { attributeToVerify } = context ;
599609 const { confirmation_code } = context . formValues ;
600610
@@ -603,7 +613,7 @@ export function signInActor({ services }: SignInMachineOptions) {
603613 confirmation_code
604614 ) ;
605615 } ,
606- async validateFields ( context , event ) {
616+ async validateFields ( context ) {
607617 return runValidators (
608618 context . formValues ,
609619 context . touched ,
0 commit comments