skip to Main Content

I am trying to verify the confirmation code for users signing up for the application using AWS Cognito in Flutter. I am receiving a Bad request, Status 400 error NotAuthorizedException (full error shown on the bottom). I set up a user pool and an app client using AWS Cognito through the AWS Dashboard. I am using the amazon_cognito_identity_dart_2 library in the Flutter application.

amazon_cognito_identity_dart_2: ^3.6.5

Below is the code. The confirmSignUp method is where I am stuck. Please insert the user pool ID, client ID, and client secret if planning on running the code:

import 'package:amazon_cognito_identity_dart_2/cognito.dart';
import 'dart:convert';

class CognitoService {
  final List<AttributeArg> userAttributes = [];
  final CognitoUserPool userPool = new CognitoUserPool(
    'UserPoolID', 'Client Id',   clientSecret: 'Client Secret');

  // Working
  Future<bool> signUp(String email, String password) async {
    try {
      await userPool.signUp(email, password);
      return true;
    } catch (e) {
      print(e);
      return false;
    }
  }

  // Below is causing the error
  Future<bool> confirmSignUp(String email, String confirmationCode) async {
    try {
      final cognitoUser = CognitoUser(email, userPool);
      return await cognitoUser.confirmRegistration(confirmationCode);
    } catch (e) {
      print(e);
      return false;
    }
  }
}

The error states:

browser_client.dart:101
POST https://cognito-idp.us-east-1.amazonaws.com/ 400 (Bad Request)

Followed by:

CognitoClientException{statusCode: 400, code: NotAuthorizedException, name: NotAuthorizedException, message: Client 4je8c3ohh0nnqogebbc2e8c8mt is configured with secret but SECRET_HASH was not received}

I enter the confirmation number but the user is never authorized.

Any advice on the matter is appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    I created a new user pool and it appears to work now. I did not enable the option to generate a client secret. Thank you for the help!


  2. as explained in the comment section can you try and implment this :

    import 'package:amazon_cognito_identity_dart_2/cognito.dart';
    import 'package:crypto/crypto.dart';
    import 'dart:convert';
    
    class CognitoService {
      final CognitoUserPool userPool = CognitoUserPool(
        'UserPoolID',
        'ClientId',
        clientSecret: 'ClientSecret',
      );
    
      String calculateSecretHash(String clientId, String clientSecret, String username) {
        final hmacSha256 = Hmac(sha256, utf8.encode(clientSecret)); // HMAC-SHA256
        final digest = hmacSha256.convert(utf8.encode(username + clientId));
        return base64Encode(digest.bytes);
      }
    
      // Working
      Future<bool> signUp(String email, String password) async {
        try {
          await userPool.signUp(email, password);
          return true;
        } catch (e) {
          print(e);
          return false;
        }
      }
    
      // Updated confirmSignUp method
      Future<bool> confirmSignUp(String email, String confirmationCode) async {
        try {
          final cognitoUser = CognitoUser(email, userPool);
          final secretHash = calculateSecretHash(userPool.getClientId(), userPool.getClientSecret(), email);
          return await cognitoUser.confirmRegistration(confirmationCode, secretHash: secretHash);
        } catch (e) {
          print(e);
          return false;
        }
      }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search