
This assumes you are familiar with Cognito. If not, check out my article FreeKB - Amazon Web Services (AWS) - Getting Started with Cognito.
This also assumes you are familiar with the basic configurations needed to connect to Amazon Web Services (AWS) using NodeJS. If not, check out my article Getting Started with NodeJS aws-sdk.
The @aws-sdk/client-cognito-identity-provider package is used to interact with Cognito in NodeJS. The npm list command can be used to determine if you have the @aws-sdk/client-cognito-identity-provider package installed. If not, the npm install command can be used to install the @aws-sdk/client-cognito-identity-provider package. Once installed, your package.json should include the @aws-sdk/client-cognito-identity-provider package.
{
"dependencies": {
"@aws-sdk/client-cognito-identity-provider": "^3.600.0"
}
}
Here is the minimal boilerplate code without any error handling to list your Cognito Pools using NodeJS CommonJS.
const { paginateListUserPools, CognitoIdentityProviderClient } = require("@aws-sdk/client-cognito-identity-provider");
const client = new CognitoIdentityProviderClient({});
export const returnUserPools = async () => {
const paginator = paginateListUserPools({ client }, {});
const userPools = [];
for await (const page of paginator) {
const names = page.UserPools.map((pool) => pool.Name);
userPools.push(...names);
}
console.log(`user pools: ${userPools}`);
};
returnUserPools();
If your app is an ES module, package.json will have "type": "module".
{
"type": "module",
"dependencies": {
"@aws-sdk/client-cognito-identity-provider": "^3.600.0"
}
}
In this scenario, you will use import instead of require.
import { paginateListUserPools, CognitoIdentityProviderClient } from "@aws-sdk/client-cognito-identity-provider";
Something like this should be returned.
~]$ node cognito.js
pools: my-cognito-pool
Did you find this article helpful?
If so, consider buying me a coffee over at