Angular / AWS Cognito User Login Error - Property 'refresh' does not exist on type 'credentials'

Viewed 1533

I am trying to configure user login for my Angular 7 application, and have constructed most of the cognitoUser.authenticateUser() method, but in the onSuccess() method, I cannot refresh the AWS config credentials because I receive the following error at AWS.config.credentials.refresh():

"Property 'refresh' does not exist on type 'Credentials | CredentialsOptions'. Property 'refresh' does not exist on type 'CredentialsOptions'."

auth.service.ts:

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

import * as AWS from 'AWS-sdk';
import { CognitoIdentityCredentials } from 'AWS-sdk';
import * as AmazonCognitoIdentity from 'amazon-cognito-identity-js';
import credentials from '../../../../server/credentials.json';

@Injectable({
  providedIn: 'root'
})

export class AuthService {

  user: AmazonCognitoIdentity.CognitoUser;

  userPool;
  userData;

  isAuthenticated: boolean;

  constructor(private router: Router) {
    const poolData = {
        UserPoolId: '*****',
        ClientId: credentials.appClientId
    }

    this.userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);

    if (this.user) {
        this.isAuthenticated = true;
    } else {
        this.isAuthenticated = false;
    }
  }

  signIn(email, password) {
    const authDetails = new AmazonCognitoIdentity.AuthenticationDetails({
        Username: email,
        Password: password
    })

    const userData = {
        Username: email,
        Pool: this.userPool
    }

    const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);

    cognitoUser.authenticateUser(authDetails, {
        onSuccess: function(res) {
            const accessToken = res.getAccessToken().getJwtToken();

            AWS.config.credentials = new AWS.CognitoIdentityCredentials({
                IdentityPoolId: this.userPool.UserPoolId,
                Logins: {
                    'cognito-idp.*****.amazonaws.com/*****': res.getIdToken().getJwtToken()
                }
            })

            AWS.config.credentials.refresh(err => {
                // ISSUE HERE
            })
        },
        onFailure: function(err) {

        }
    }
  }

}
2 Answers
AWS.config.credentials.refresh(err => {
  // ISSUE HERE
})

replace the above code with this

( < AWS.CognitoIdentityCredentials > AWS.config.credentials).refresh((error) => {

});

This is called casting of variable

I had issues with the casting solution from @Prashant Tapase. This worked for me.

    const cognitoIdentityCredentials = new AWS.CognitoIdentityCredentials({
      ...
    });

    AWS.config.credentials = cognitoIdentityCredentials;

    cognitoIdentityCredentials.refresh((error) => {
      ...
    });
Related