async function ensureCognitoUserPoolDomain()

in src/cfn-custom-resources/user-pool-domain/index.ts [20:48]


async function ensureCognitoUserPoolDomain(
  action: "Create" | "Update" | "Delete",
  newUserPoolArn: string,
  physicalResourceId?: string
) {
  if (action === "Delete") {
    return physicalResourceId!;
  }
  const newUserPoolId = newUserPoolArn.split("/")[1];
  const newUserPoolRegion = newUserPoolArn.split(":")[3];
  const cognitoClient = new CognitoIdentityServiceProvider({
    region: newUserPoolRegion,
  });
  const { UserPool } = await cognitoClient
    .describeUserPool({ UserPoolId: newUserPoolId })
    .promise();
  if (!UserPool) {
    throw new Error(`User Pool ${newUserPoolArn} does not exist`);
  }
  if (UserPool.CustomDomain) {
    return UserPool.CustomDomain;
  } else if (UserPool.Domain) {
    return `${UserPool.Domain}.auth.${newUserPoolRegion}.amazoncognito.com`;
  } else {
    throw new Error(
      `User Pool ${newUserPoolArn} does not have a domain set up yet`
    );
  }
}