首页 > 解决方案 > Angular / AWS Cognito 用户登录错误-“凭据”类型上不存在属性“刷新”

问题描述

我正在尝试为我的 Angular 7 应用程序配置用户登录,并且已经构建了大部分cognitoUser.authenticateUser()方法,但是在该onSuccess()方法中,我无法刷新 AWS 配置凭据,因为我在以下位置收到以下错误AWS.config.credentials.refresh()

“'Credentials | CredentialsOptions' 类型上不存在属性 'refresh'。'CredentialsOptions' 类型上不存在属性 'refresh'。”

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) {

        }
    }
  }

}

标签: angularamazon-web-servicesaws-lambdaamazon-cognitoangular7

解决方案


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

用这个替换上面的代码

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

});

这称为变量转换


推荐阅读