amazon-cognito-identity-js icon indicating copy to clipboard operation
amazon-cognito-identity-js copied to clipboard

Property 'get' does not exist on type 'Credentials | CredentialsOptions'.

Open akashbiz opened this issue 6 years ago • 3 comments

Hi, I am importing this library via npm and used Angular2 Cognito Quickstart for reference but I also need to use cognitosync library which get called immediately after login. Below is the code I am trying for which I am facing many issues

  1. Property 'get' does not exist on type 'Credentials | CredentialsOptions'.
  2. Property 'identityId' does not exist on type 'Credentials | CredentialsOptions'.
  3. Property 'CognitoSyncManager' does not exist on type 'typeof ".../node_modules/aws-sdk/global"'.

---------cognito.service.ts--------- ` import { AuthenticationDetails, CognitoIdentityServiceProvider, CognitoUser, CognitoUserAttribute, CognitoUserPool } from "amazon-cognito-identity-js"; import * as AWS from "aws-sdk/global"; import * as STS from "aws-sdk/clients/sts"; import * as awsservice from "aws-sdk/lib/service"; import * as CognitoIdentity from "aws-sdk/clients/cognitoidentity";

buildCognitoCreds( idTokenJwt: string ) { let url = 'cognito-idp.' + Environment.REGION.toLowerCase() + '.amazonaws.com/' + Environment.USER_POOL_ID; if ( Environment.COGNITO_IDP_ENDPOINT ) { url = Environment.COGNITO_IDP_ENDPOINT + '/' + Environment.USER_POOL_ID; } let logins: CognitoIdentity.LoginsMap = {}; logins[url] = idTokenJwt; let params = { IdentityPoolId: Environment.IDENTITY_POOL_ID, Logins: logins }; let serviceConfigs: awsservice.ServiceConfigurationOptions = {}; if ( Environment.COGNITO_IDENTITY_ENDPOINT ) { serviceConfigs.endpoint = Environment.COGNITO_IDENTITY_ENDPOINT; } let creds = new AWS.CognitoIdentityCredentials( params, serviceConfigs ); this.setCognitoCreds( creds ); return creds; }

authenticate( username: string, password: string ): Promise {

let authenticationData = {
	Username: username,
	Password: password,
};
let authenticationDetails = new AuthenticationDetails( authenticationData );

let userData = {
	Username: username,
	Pool: this.getUserPool()
};
let cognitoUser = new CognitoUser( userData );
var self = this;
return new Promise(
	( resolve, reject ) => {
		cognitoUser.authenticateUser( authenticationDetails, {
			onSuccess: function( result ) {
				let creds = self.buildCognitoCreds( result.getIdToken().getJwtToken() );
				console.log(creds);
				AWS.config.credentials = creds;
				let clientParams: any = {};
				if ( Environment.STS_ENDPOINT ) {
					clientParams.endpoint = Environment.STS_ENDPOINT;
				}
				let sts = new STS( clientParams );
				sts.getCallerIdentity( function( err, data ) {
					console.log( "CognitoService: Successfully set the AWS credentials", data );
					self.getUserAttributes()
						.then( results => {
							console.log('user attributes==>', results);
							resolve( result );
						} )
						.catch( error => { } );
				} );

			},
			onFailure: function( error ) {
				reject( error );
			}
		} );
	} );

}`

---------cognito-sync.service--------- `import * as AWS from "aws-sdk/global"; import * as CognitoSync from "aws-sdk/clients/cognitosync"; import { CognitoSyncManager } from 'amazon-cognito-js';

initSync(): void { this.cognitosync = new CognitoSync( { correctClockSkew: true } ); this.cognitoSyncManager = new AWS.CognitoSyncManager(); // 3rd issue }

getAllDatasets(): Promise<DataSets> {
    return new Promise(
        ( resolve, reject ) => {
            let promise = AWS.config.credentials.getPromise(); // **1st issue**
            promise.then(() => {
            console.log(AWS.config.credentials);
                let identityID = AWS.config.credentials.identityId; // **2nd Issue**
                let params = {
                    IdentityId: identityID,
                    IdentityPoolId: Environment.IDENTITY_POOL_ID
                };
                this.cognitosync.listDatasets( params, ( error, data ) => {
                    if ( error ) {
                        console.error( error );
                        reject( error );
                    }
                    else {
                        resolve( data );
                    }
                } );
            } ).catch( error => {
                console.error( error );
            } );
        } );
}`

Please help.

akashbiz avatar Sep 11 '17 11:09 akashbiz

You should use new AWS.CognitoSync() instead of new AWS.CognitoSyncManager().

This blog post walks through the setup and example of using the Cognito Sync JavaScript library. It can help you with errors you are facing.

chetanme avatar Dec 21 '17 00:12 chetanme

Thanks for response @chetanme, I will try it out and get back to you with results.

akashbiz avatar Dec 21 '17 08:12 akashbiz

The following might help for your first issue. let creds:AWS.CognitoIdentityCredentials = new AWS.CognitoIdentityCredentials(params);

   ` console.log("creds",creds);

    creds.get(function(err) {
      if (err) return console.log("Error", err);
      else {
        console.log("Sucuessfully ");
       console.log("Cognito Identity Id", creds.identityId);
       console.log("token",creds.sessionToken);
      }
    });```

joe455 avatar Dec 26 '17 13:12 joe455