def create_and_authenticate_user()

in lib/cognito_util.py [0:0]


def create_and_authenticate_user(userPoolId, appClientId, username, password, userAttributes=None):
  # Create the user
  response = cognitoIdp.admin_create_user(
    UserPoolId=userPoolId,
    Username=username,
    UserAttributes=userAttributes, 
    TemporaryPassword=password,
    MessageAction='SUPPRESS'
  )
      
  # Initiate the authentication
  response = cognitoIdp.admin_initiate_auth(
    UserPoolId=userPoolId,
    ClientId=appClientId,
    AuthFlow='ADMIN_USER_PASSWORD_AUTH',
    AuthParameters={
        'USERNAME': username,
        'PASSWORD': password
    }
  )
  session = response['Session']
  
  # Respond to challenge
  response = cognitoIdp.admin_respond_to_auth_challenge(
    UserPoolId=userPoolId,
    ClientId=appClientId,
    ChallengeName='NEW_PASSWORD_REQUIRED',
    ChallengeResponses={
      'USERNAME': username,
      'NEW_PASSWORD': password
    },
    Session=session
  )
  
  time.sleep(2) # Give a little time to finish