furaiev / amazon-cognito-identity-dart-2

Unofficial Amazon Cognito Identity Provider Dart SDK, to easily add user sign-up and sign-in to your mobile and web apps with AWS.
MIT License
187 stars 114 forks source link

MFA error When trying to use otp #151

Closed gregorysaml closed 3 years ago

gregorysaml commented 3 years ago

Hi, I am trying to login with otp using this fuction

Future<User> login(String phone, String password) async {

    _cognitoUser = CognitoUser(phone, _userPool, storage: _userPool.storage);

    final authDetails =
        AuthenticationDetails(username: phone, password: fixedPassword);
    bool isConfirmed;

    try {
      _session = await _cognitoUser?.authenticateUser(authDetails);
      isConfirmed = true;
    } on CognitoUserMfaRequiredException catch (e) {
      if (e.challengeName == 'SMS_MFA') {
        isConfirmed = false;
        print(e);
      } else {
        print('$e');

      }
    } on CognitoUserSelectMfaTypeException catch (e) {
      print(e);
      // handle SELECT_MFA_TYPE challenge
    } on CognitoUserMfaSetupException catch (e) {
      // handle MFA_SETUP challenge
      print(e);
    } on CognitoUserTotpRequiredException catch (e) {
      // handle SOFTWARE_TOKEN_MFA challenge
      print(e);
    } on CognitoUserCustomChallengeException catch (e) {
      // handle CUSTOM_CHALLENGE challenge
      print(e);
    } on CognitoUserConfirmationNecessaryException catch (e) {
      // handle User Confirmation Necessary
      print(e);
    } on CognitoClientException catch (e) {
      // handle Wrong Username and Password and Cognito Client
      print(e);
    }

    if (!_session.isValid() || _session == null) {
      return null;
    }

    final attributes = await _cognitoUser?.getUserAttributes();
    if (attributes != null) {
      final user = User.fromUserAttributes(attributes);
      user.confirmed = isConfirmed;
      user.hasAccess = true;
      return user;
    } else {
      return null;
    }
  }

but i am getting this error

116269518-f69ef180-a786-11eb-8bbe-428f2fdd11cc

I tried to catch the error in the package with no success

cognito_user.dart

Future<CognitoUserSession?> _authenticateUserInternal( dataAuthenticate, AuthenticationHelper authenticationHelper) async {
    final String? challengeName = dataAuthenticate['ChallengeName'];  
    var challengeParameters = dataAuthenticate['ChallengeParameters'];

    if (challengeName == 'SMS_MFA') {
      try {
        _session = dataAuthenticate['Session'];
      } on CognitoUserMfaRequiredException catch (e) {
        challengeName:
        challengeName;
        challengeParameters:
        challengeParameters;
        print(e);
      }
    } 
With try and catch i am getting this error 

Id token null 2

Are there any missing parts in the sdk regarding mfa verification ? Because when i am getting the sms and immediately the sdk throws this error above (SMS MFA ) Also you can see how i implement my pool here #141

Thanks in advance for your support !