What is @aws-sdk/client-cognito-identity-provider?
@aws-sdk/client-cognito-identity-provider is a part of the AWS SDK for JavaScript. It allows developers to interact with Amazon Cognito, a service that provides user sign-up, sign-in, and access control. This package enables you to manage user pools, authenticate users, and handle user data securely.
What are @aws-sdk/client-cognito-identity-provider's main functionalities?
User Sign-Up
This feature allows you to register a new user in a Cognito User Pool. The code sample demonstrates how to use the SignUpCommand to create a new user with specified attributes.
const { CognitoIdentityProviderClient, SignUpCommand } = require('@aws-sdk/client-cognito-identity-provider');
const client = new CognitoIdentityProviderClient({ region: 'us-west-2' });
const signUp = async () => {
const params = {
ClientId: 'your-client-id',
Username: 'username',
Password: 'password',
UserAttributes: [
{
Name: 'email',
Value: 'user@example.com'
}
]
};
const command = new SignUpCommand(params);
try {
const data = await client.send(command);
console.log('Sign-Up successful:', data);
} catch (error) {
console.error('Error during sign-up:', error);
}
};
signUp();
User Authentication
This feature allows you to authenticate a user using their username and password. The code sample demonstrates how to use the InitiateAuthCommand to authenticate a user and retrieve authentication tokens.
const { CognitoIdentityProviderClient, InitiateAuthCommand } = require('@aws-sdk/client-cognito-identity-provider');
const client = new CognitoIdentityProviderClient({ region: 'us-west-2' });
const authenticateUser = async () => {
const params = {
AuthFlow: 'USER_PASSWORD_AUTH',
ClientId: 'your-client-id',
AuthParameters: {
USERNAME: 'username',
PASSWORD: 'password'
}
};
const command = new InitiateAuthCommand(params);
try {
const data = await client.send(command);
console.log('Authentication successful:', data);
} catch (error) {
console.error('Error during authentication:', error);
}
};
authenticateUser();
Password Reset
This feature allows you to initiate a password reset for a user. The code sample demonstrates how to use the ForgotPasswordCommand to start the password reset process, which sends a verification code to the user's email or phone.
const { CognitoIdentityProviderClient, ForgotPasswordCommand } = require('@aws-sdk/client-cognito-identity-provider');
const client = new CognitoIdentityProviderClient({ region: 'us-west-2' });
const resetPassword = async () => {
const params = {
ClientId: 'your-client-id',
Username: 'username'
};
const command = new ForgotPasswordCommand(params);
try {
const data = await client.send(command);
console.log('Password reset initiated:', data);
} catch (error) {
console.error('Error during password reset:', error);
}
};
resetPassword();
Other packages similar to @aws-sdk/client-cognito-identity-provider
amazon-cognito-identity-js
The amazon-cognito-identity-js package is a JavaScript library for Amazon Cognito. It provides similar functionalities to @aws-sdk/client-cognito-identity-provider, such as user sign-up, sign-in, and password management. However, it is more focused on client-side applications and provides a higher-level abstraction for interacting with Cognito.
cognito-express
cognito-express is a middleware for Express.js that simplifies the process of verifying Amazon Cognito JWT tokens. It is useful for server-side applications that need to authenticate and authorize users based on their Cognito tokens. Unlike @aws-sdk/client-cognito-identity-provider, it does not provide user management functionalities.
aws-amplify
AWS Amplify is a comprehensive library that provides a set of tools and services for building cloud-enabled applications. It includes support for Amazon Cognito for authentication and user management, along with other AWS services. AWS Amplify offers a higher-level abstraction and is designed to simplify the integration of various AWS services into your application.
@aws-sdk/client-cognito-identity-provider
Description
AWS SDK for JavaScript CognitoIdentityProvider Client for Node.js, Browser and React Native.
Using the Amazon Cognito User Pools API, you can create a user pool to manage
directories and users. You can authenticate a user to obtain tokens related to user
identity and access policies.
This API reference provides information about user pools in Amazon Cognito User
Pools.
For more information, see the Amazon Cognito Documentation.
Installing
To install the this package, simply type add or install @aws-sdk/client-cognito-identity-provider
using your favorite package manager:
npm install @aws-sdk/client-cognito-identity-provider
yarn add @aws-sdk/client-cognito-identity-provider
pnpm add @aws-sdk/client-cognito-identity-provider
Getting Started
Import
The AWS SDK is modulized by clients and commands.
To send a request, you only need to import the CognitoIdentityProviderClient
and
the commands you need, for example AddCustomAttributesCommand
:
const {
CognitoIdentityProviderClient,
AddCustomAttributesCommand,
} = require("@aws-sdk/client-cognito-identity-provider");
import { CognitoIdentityProviderClient, AddCustomAttributesCommand } from "@aws-sdk/client-cognito-identity-provider";
Usage
To send a request, you:
- Initiate client with configuration (e.g. credentials, region).
- Initiate command with input parameters.
- Call
send
operation on client with command object as input. - If you are using a custom http handler, you may call
destroy()
to close open connections.
const client = new CognitoIdentityProviderClient({ region: "REGION" });
const params = {
};
const command = new AddCustomAttributesCommand(params);
Async/await
We recommend using await
operator to wait for the promise returned by send operation as follows:
try {
const data = await client.send(command);
} catch (error) {
} finally {
}
Async-await is clean, concise, intuitive, easy to debug and has better error handling
as compared to using Promise chains or callbacks.
Promises
You can also use Promise chaining
to execute send operation.
client.send(command).then(
(data) => {
},
(error) => {
}
);
Promises can also be called using .catch()
and .finally()
as follows:
client
.send(command)
.then((data) => {
})
.catch((error) => {
})
.finally(() => {
});
Callbacks
We do not recommend using callbacks because of callback hell,
but they are supported by the send operation.
client.send(command, (err, data) => {
});
v2 compatible style
The client can also send requests using v2 compatible style.
However, it results in a bigger bundle size and may be dropped in next major version. More details in the blog post
on modular packages in AWS SDK for JavaScript
import * as AWS from "@aws-sdk/client-cognito-identity-provider";
const client = new AWS.CognitoIdentityProvider({ region: "REGION" });
try {
const data = client.addCustomAttributes(params);
} catch (error) {
}
client
.addCustomAttributes(params)
.then((data) => {
})
.catch((error) => {
});
client.addCustomAttributes(params, (err, data) => {
});
Troubleshooting
When the service returns an exception, the error will include the exception information,
as well as response metadata (e.g. request id).
try {
const data = await client.send(command);
} catch (error) {
const { requestId, cfId, extendedRequestId } = error.$metadata;
console.log({ requestId, cfId, extendedRequestId });
}
Getting Help
Please use these community resources for getting help.
We use the GitHub issues for tracking bugs and feature requests, but have limited bandwidth to address them.
To test your universal JavaScript code in Node.js, browser and react-native environments,
visit our code samples repo.
Contributing
This client code is generated automatically. Any modifications will be overwritten the next time the @aws-sdk/client-cognito-identity-provider
package is updated.
To contribute to client you can check our generate clients scripts.
License
This SDK is distributed under the
Apache License, Version 2.0,
see LICENSE for more information.