Docs/Amazon cognito

Amazon Cognito

The Amazon Cognito

is a that automatically imports your users and keeps them up-to-date in UserHub.

To enable the Amazon Cognito connection, you'll need access to an Amazon Cognito user pool and the ability to create an access key with the appropriate permissions.

Get Amazon Cognito user pool ID

  1. Go to the Cognito dashboard
  2. Switch to to the region with the user pool you want to connect to UserHub
  3. Note the User pool ID, you'll need it for the Set up connection step

Create AWS access key

  1. Go to the IAM users dashboard
  2. Click Create user to add a new access key
  3. Enter userhub for the User name and click Next
  4. Select Attach policies directly
  5. Search for the AmazonCognitoPowerUser policy name (you can also create a more restrictive custom policy, see the example policy)
  6. Check the checkbox to the left of the policy name and click Next
  7. Click Create user
  8. Search for userhub and click on it
  9. Click the Security credentials tab
  10. Scroll down to the Access key section and click Create access key
  11. Select Third-party service, check the Confirmation checkbox, and click Next
  12. Click Create access key
  13. Note the Access key and Secret access key, you'll need it in the next step

Set up connection

Next, you will need to set up the

in the UserHub .

  1. Go to Connections from the Developers dropdown
  2. Click the Setup button for Amazon Cognito
  3. Enter your Amazon Cognito User pool ID
  4. Enter the Region for your user pool
  5. Enter the Access key ID and Access key secret you created above
  6. Click Save
  7. Make sure the status switches to Active

Make note of the connection ID (e.g. conn_obXdd...), as this will be used in a later step.

Set up Portal callback

Next, you will use Amazon Cognito's sign-in interface to authenticate users linked to the UserHub

.

Alternatively, you have the option of implementing the Portal callback handler.

Create app for callback

  1. Go to the Cognito dashboard
  2. Click on your User pool name
  3. Click App clients in the navigation menu
  4. Click the Create app client button
  5. Select Traditional web application for Application type
  6. Enter Billing (or whatever you prefer) for Name your application
  7. Enter https://<portal-domain>/signin for the Return URL (you can find this URL in the Portal settings)
  8. Click Create app client
  9. Click the Login pages tab
  10. Click Edit in the Managed login pages configuration section
  11. Click Add sign-out URL and enter https://<portal-domain>/signout
  12. Scroll to the bottom of the page and click Save changes
  13. Note the Client ID and Client secret, you'll need it in the next step

Add app settings to connection

  1. Go to Connections from the UserHub Developers dropdown
  2. Click Amazon Cognito
  3. Click the Edit connection button
  4. Click Enable Portal sign-in
  5. Enter the Client ID and Client secret you noted above and click Save

You should now be able to open https://<portal-domain>/ in your browser and sign in.

Set up sign-in syncing

Setting up a Lambda trigger ensures your users are immediately pushed to UserHub when they sign in.

To set up a trigger, you'll need the UserHub Amazon Cognito connection ID (see above) and an

key scoped to Amazon Cognito.

Alternatively, you have the option of calling report event directly from your app.

Create API key

  1. Go to the and click API keys via the Developers dropdown or Tenant settings
  2. Click New API key
  3. Enter Amazon Cognito hook for the Description
  4. Select Admin API for the Type
  5. Don't set an expiration
  6. Click the Integrations dropdown and select Amazon Cognito
  7. Click Create
  8. Note the generated API key (e.g. userhub_admin_agX...), as this token will be used in the next step

Set up trigger

Next, you'll need to set up the trigger in Cognito:

  1. Go to the Cognito dashboard

  2. Click on your User pool name

  3. Click Extensions under Authentication

  4. Click the Add Lambda trigger button

  5. Select Authentication for the Trigger type

  6. Select Post authentication trigger for Authentication

  7. Click the Create Lambda function button

  8. In the new tab, click the Create function button

  9. Select Author from Scratch

  10. Enter userhubSignin for the Function name (or whatever you prefer)

  11. Select Node.js 22.x (or higher) for the Runtime

  12. Select x86_64 for Architecture

  13. Click the Create function button

  14. Enter the following code:

    const baseUrl = "https://api.userhub.com";
    
    export const handler = async event => {
      try {
        const userId = encodeURIComponent(event.userName);
        const connectionId = process.env.AMAZON_COGNITO_CONNECTION_ID;
    
        const response = await fetch(
          `${baseUrl}/admin/v1/users/${userId}@${connectionId}:event`,
          {
            method: "POST",
            headers: {
              authorization: `Bearer ${process.env.USERHUB_ADMIN_KEY}`,
              "user-agent": "UserHub-AmazonCognito/1.0",
            },
            data: JSON.stringify({ type: "SIGNIN" }),
            signal: AbortSignal.timeout(2 * 1000),
          },
        );
        if (!response.ok) {
          throw new Error(`${response.status}: ${await response.text()}`);
        }
      } catch (e) {
        console.log(`Failed to report signin to UserHub: ${e}`);
      }
    
      return event;
    };
    
  15. Click the Configuration tab

  16. Click Environment variables from the side menu and the Edit button

  17. Click the Add environment variable button

  18. Enter USERHUB_ADMIN_KEY for the Key

  19. Enter the Admin API key you generated above for the Value

  20. Click Add environment variable again

  21. Enter AMAZON_COGNITO_CONNECTION_ID for the Key

  22. Enter the connection ID you noted in step 2 for the Value (e.g. conn_obXdd...)

  23. Click Save

  24. Click the Code tab

  25. Click the Deploy button

  26. Switch back to the "Add Lambda trigger" browser tab

  27. Click the refresh icon

  28. Select userhubSignin (or whatever you choose) for Assign Lambda function

  29. Click Add Lambda trigger

PreviousClient SDKs
NextAuth0

Build software thatmonetizes
$

Subscribe to monthly product updates

© 2025 UserHub

Integrations

    UserHub & Auth0UserHub & Stripe BillingUserHub & Google CloudUserHub & FirebaseUserHub & custom auth