Search code examples
node.jsamazon-web-servicesaws-sdk-js

Do I need amazon-cognito-identity-js if I already have the aws-sdk (javascript)?


I have a javascript project where I use the aws-sdk. No I want to use amazon-cognito-identity-js. On the page it says:

Note that the Amazon Cognito AWS SDK for JavaScript is just a slimmed down version of the AWS Javascript SDK namespaced as AWSCognito instead of AWS. It references only the Amazon Cognito Identity service.

and indeed, I can for example create CognitoIdentityServiceProvider with:

CognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider();

But how do I do thinks like authenticate a user? According to the amazon-cognito-identity-js documentation:

authenticationDetails = new CognitoIdentityServiceProvider.AuthenticationDetails({Userame: ..., Password: ...});
cognitoUser.authenticateUser(authenticationDetails, ...)

But the CognitoIdentityServiceProvider object does not have a AuthenticationDetails property.

Do I have to do something different when I use the aws-sdk instead of amazon-cognito-identity-js?

Or is my assumption wrong, and I need both, the aws-sdk and amazon-cognito-identity-js?


Solution

  • No, you don't. You can login using aws-sdk like this:

    const cognito = new aws.CognitoIdentityServiceProvider({ region });
    cognito.adminInitiateAuth({
      AuthFlow: 'ADMIN_NO_SRP_AUTH',
      ClientId: clientId,
      UserPoolId: poolId,
      AuthParameters: {
        USERNAME: email,
        PASSWORD: password,
      },
    });