Search code examples
node.jsreactjsauthenticationnestjsamazon-cognito

Create Different AWS Cognito user pools, roles according to tenant with NestJS/Node.js


I am developing a multi tenant application using NestJS (Node.js) and react. I am looking to store my users in the AWS Cognito user pool. I need to keep my users in different user pools according to their tenants.And need to create roles according to their tenants. I did not able to find a way to create user pools, roles and users through the Node.js (NestJS) or react. I did not find any helpful way to do that. Can some one advice me how to create different user pools through the program.(Is it possible??)

I followed some tutorials but I am not able to find a proper solution. And authorisation is and authentication looks fine. But I need to keep my users in different user groups according to their tenants.


Solution

  • Yes, we can do this programmatically. AWS Cognito user pools APIs facilitate this. For an example we can use CreateUserPool endpoint, to create a new user pool.

    For Node.js, you can rely on AWS JavaScript SDK, which simplifies the usage of above APIs even more.

    You have to use CognitoIdentityProviderClient and send a CreateUserPoolCommand with relevant parameters.

    Following code block illustrate the high-level approach.

    //Initiates the client with configs.
    const client = new CognitoIdentityProviderClient({
        region: "REGION"
    });
    
    //setup input params for CreateUserPoolCommandInput
    const params = {
        PoolName: "the-name-of-the-pool",
        EmailVerificationMessage: "message goes here."
        //add rest of the details as required.
    };
    
    //Initiate the command with input parameters.
    const command = new CreateUserPoolCommand(params);
    
    //Call send operation on client with command object as input.
    const data = await client.send(command);
    

    Hope this would help you to achieve your requirement. Please refer given links for more details.