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
182 stars 113 forks source link

CognitoUserException: User Confirmation Necessary #210

Closed omerfaunal closed 1 year ago

omerfaunal commented 1 year ago

I'm trying authenticate user with the code below. When I send a request for the first time, it raises the "CognitoUserException: User Confirmation Necessary" error even though the user is confirmed. When I send an authenticate request for the second time without doing anything else, it accepts the user. So basically if I run the code twice, everything is fine but in the first time it raises an exception.

Where could this situation come from?

Future<User> authenticate(String username, String password) async {

    user = User();
    final cognitoUser = CognitoUser(username, userPool);
    final authDetails = AuthenticationDetails(
      username: username,
      password: password,
    );
    user.userName = username;
    user.password = password;
    CognitoUserSession? session;
    try {
      session = await cognitoUser.authenticateUser(authDetails);
    } on CognitoUserNewPasswordRequiredException catch (e) {
      user.firstTime = true;
      return user;
    } on CognitoClientException catch (e) {
      if (e.message == "Unknown Error") {
        rethrow;
      }
      throw WrongPasswordException();
    } catch (e) {
      print(e);
    }
    }

And another interesting part is that I'm testing the app using two different Amazon accounts, and in one account with the same cognito settings there is no problem. I can directly authenticate the user, but in another account there is the problem thar I mentioned.