jphorta
jphorta

Reputation: 2174

Cognito admin_initiate_auth responds with exception User does not exist when creating a new user

I'm trying to create a new user in a Cognito user pool from my ruby backend server. Using this code:

client = Aws::CognitoIdentityProvider::Client.new
response = client.admin_initiate_auth({
  auth_flow: 'ADMIN_NO_SRP_AUTH',
  auth_parameters: {
    'USERNAME': @user.email,
    'PASSWORD': '123456789'
  },
  client_id: ENV['AWS_COGNITO_CLIENT_ID'],
  user_pool_id: ENV['AWS_COGNITO_POOL_ID']
})

The response I get is Aws::CognitoIdentityProvider::Errors::UserNotFoundException: User does not exist.

I'm trying to follow the Server Authentication Flow (https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html), and from that I understood that I could create a new user using admin_initiate_auth.

Am I doing something wrong here?

Thanks

Upvotes: 0

Views: 2698

Answers (1)

Liam
Liam

Reputation: 1920

You're using the wrong method. admin_initiate_auth is for logging in/authenticating a user with the ADMIN_NO_SRP_AUTH turned on.

You need to use the sign_up method:

resp = client.sign_up({
  client_id: "ClientIdType", # required
  secret_hash: "SecretHashType",
  username: "UsernameType", # required
  password: "PasswordType", # required
  user_attributes: [
    {
      name: "AttributeNameType", # required
      value: "AttributeValueType",
    },
  ],
  validation_data: [
    {
      name: "AttributeNameType", # required
      value: "AttributeValueType",
    },
  ],
  analytics_metadata: {
    analytics_endpoint_id: "StringType",
  },
  user_context_data: {
    encoded_data: "StringType",
  },
})

You can find it in the AWS Cognito IDP docs here.

Upvotes: 1

Related Questions