Search code examples
amazon-web-servicestypescriptrequirejsamazon-cognito

Uncaught: TypeError: AWS.CognitoIdentityCredentials is not a constructor


I got an issue with a custom build AWS-SDK. Maybe it´s just a stupid issue by me not seeing a tree in the forest but it drives me crazy. So here we got.

I did build based on the core version 2.247.1 an sdk with all Cognito and DynamoDB services like Amazon tells us here.

Then I´ve tried to require it in my code as follows:

const AWS = require('../../../../assets/scripts/aws-sdk-2.247.1.js');

Further more I followed the example implementation that AWS shows us here.

So I came up with this code to get a session to an already logged in user:

getUserSession(
        response: ICognitoResponse,
        callback: ( callbackResponse: ICognitoResponse ) => {} ) {

        // Validate the Usersession
        this.cognitoUser.getSession((err: any, session: any) => {
            if (err) {
                response = assign(response, { err });
                callback( response );
                return;
            } else {
                /**
                 * Set the right URL
                 * @type {string}
                 */
                const URL = 'cognito-idp.' +
                    environment.AWS_REGION +
                    '.amazonaws.com/' +
                    environment.USERPOOL_ID;

                /**
                 * Update the Credentials with the current updated URL
                 * @type {AWS.CognitoIdentityCredentials}
                 */
                AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                    /**
                     * your identity pool id here
                     */
                    IdentityPoolId: environment.USERPOOL_ID,
                    Logins: {

                        /**
                         * Change the key below according to the
                         * specific region your user pool is in.
                         */
                        URL: session.getIdToken().getJwtToken(),
                    },
                });
            }
        });
    }

It compiles without any error and I can login. BUT directly after this, I get the following error:

Uncaught: TypeError: AWS.CognitoIdentityCredentials is not a constructor

If I use the same code with the complete javascript SDK, which is by the way insane huge, everything works fine.

Hopefully some of you can help me out. I did try serveral different import techniques like import * as AWS and so on. Nothing worked.


Solution

  • I got it. First import the library in the script tg in the index.html. Then add the following to the ts-File:

    declare var AWS: any
    

    After that it´s possible to use it as AWS.config and AWS.CognitoIdentityCredentials in an Angular 5 App.