amazon-archives / amazon-cognito-js

Amazon Cognito Sync Manager for JavaScript
http://aws.amazon.com/cognito
Apache License 2.0
202 stars 83 forks source link

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

Open akashbiz opened 6 years ago

akashbiz commented 6 years ago

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

Property 'get' does not exist on type 'Credentials | CredentialsOptions'. Property 'identityId' does not exist on type 'Credentials | CredentialsOptions'. 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.

Leon-Africa commented 6 years ago

Hi there, please forward the following:

  1. The entire error log
  2. Log the AWS.config object
  3. Log the AWS.config.credentials object
akashbiz commented 6 years ago

I followed this[https://github.com/aws/amazon-cognito-js/issues/40] link to create cognitoSyncManager object import as AWS from "aws-sdk/global"; import as CognitoSync from "aws-sdk/clients/cognitosync"; import { CognitoSyncManager } from 'amazon-cognito-js';

this.cognitoSyncManager = new AWS.CognitoSyncManager()

Error: Property 'CognitoSyncManager' does not exist on type 'typeof ".../node_modules/aws-sdk/global"'.


AWS.config object: { "credentials": { "expired": true, "expireTime": null, "params": { "IdentityPoolId": "IDENTITY_POOL_ID", "Logins": { "cognito-idp.us-east-1.amazonaws.com/USER_POOL_ID": "accessToken" } }, "data": null, "_identityId": null, "_clientConfig": {} }, "credentialProvider": null, "region": "us-east-1", "logger": null, "apiVersions": {}, "apiVersion": null, "httpOptions": { "timeout": 120000 }, "maxRedirects": 10, "paramValidation": true, "sslEnabled": true, "s3ForcePathStyle": false, "s3BucketEndpoint": false, "s3DisableBodySigning": true, "computeChecksums": true, "convertResponseTypes": true, "correctClockSkew": false, "customUserAgent": null, "dynamoDbCrc32": true, "systemClockOffset": 0, "signatureVersion": null, "signatureCache": true, "retryDelayOptions": {}, "useAccelerateEndpoint": false }


AWS.config.credentials object: { "expired": true, "expireTime": null, "params": { "IdentityPoolId": "IDENTITY_POOL_ID", "Logins": { "cognito-idp.us-east-1.amazonaws.com/USER_POOL_ID": "idToken" } }, "data": null, "_identityId": null, "_clientConfig": {} }


Also, when I am trying to copy identityId from credentials object in following way let identityID = AWS.config.credentials.identityId; and encounter an issue saying Property 'identityId' does not exist on type 'Credentials | CredentialsOptions'. Property 'identityId' does not exist on type 'Credentials'.

wmagda commented 6 years ago

I've run into the same issue. The problem is that identityId, get and other are part of CognitoIdentityCredentials and not Credentials | CredentialsOptions as typescript compiler thinks. I was able to fix this by simply casting my object to the correct type: `

AWS.config.credentials).identityId ` This worked for me. Cheers!
akashbiz commented 6 years ago

Thanks @wmagda, will try this solution.

juanfjk commented 6 years ago

Thanks @wmagda works fine your solution. For someone that has problems with the function AWS.config.credentials.get(function(err, data) I solved with the function AWS.config.getCredentials(function(err) that is documented here.

swaminathangunasekeran commented 6 years ago

HI All, I am really stuck here, can some one help me .i don't know am i casting it correctly i just casted my object like below but still throwing me error which i mentioned below my code

let params = { IdentityPoolId: CognitoUtil._IDENTITY_POOL_ID, / required / Logins: { 'graph.facebook.com': token } }; AWS.config.credentials = new AWS.CognitoIdentityCredentials(params); AWS.config.credentials.refresh((error) => { if (error) { console.error(error); } else { console.log('Successfully logged!'); } }); ERROR : Property 'refresh' does not exist on type 'Credentials | CredentialsOptions'.

swaminathangunasekeran commented 6 years ago

I fixed this by ( AWS.config.credentials).get((error) => { // }) i am getting error as null now ,but i dont know how to processd further , for userpool credentials i am calling this.userService.isAuthenticated() to know user authenticated or not .. now how can i know is user authenticated or not ,,as above function still throwing error