加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在Angular 2中实现AWS-Cognito

发布时间:2020-12-17 17:57:02 所属栏目:安全 来源:网络整理
导读:我目前正在尝试使用Angular和AWS构建Web应用程序.我的第一步是使用AWS-Cognito进行工作身份验证.但是我在使用AWS-Cognito SDK时遇到了一些问题. 我采取了以下步骤: 我开始使用这个Angular 2快速入门来设置我的应用程序:https://github.com/angular/quickst
我目前正在尝试使用Angular和AWS构建Web应用程序.我的第一步是使用AWS-Cognito进行工作身份验证.但是我在使用AWS-Cognito SDK时遇到了一些问题.

我采取了以下步骤:

我开始使用这个Angular 2快速入门来设置我的应用程序:https://github.com/angular/quickstart,然后运行npm install

我的下一步是使用npm install -g @ angular / cli安装angular CLI

接下来我通过运行安装angular-cognito-identity-sdk:npm install –save amazon-cognito-identity-js

安装SDK后,我需要将sdk放入我的组件中:

console.log(AmazonCognitoIdentity);

         var authenticationData = {
            Username : 'username',Password : 'password',};
        var authenticationDetails = new AmazonCognitoIdentity.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
        var poolData = {
            UserPoolId : 'pool_id',// Your user pool id here
            ClientId : 'client_id' // Your client id here
        };
        var userPool = new AmazonCognitoIdentity.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
        var userData = {
            Username : 'username',Pool : userPool
        };

但是当我运行代码时,I?m给出了以下错误:

TypeError: Cannot read property ‘AuthenticationDetails’ of undefined

我在这里错过了一步吗?在我的Angular应用程序中实现Cognito SDK的最佳方法是什么?

谢谢!

解决方法

删除CognitoIdentityServiceProvider.例如:

import * as AWSCognito from 'amazon-cognito-identity-js';

// Later on
const userPool = new AWSCognito.CognitoUserPool(awsCognitoSettings);
const authDetails = new AWSCognito.AuthenticationDetails({
  Username: this.state.username,Password: this.state.password
});
const cognitoUser = new AWSCognito.CognitoUser({
  Username: this.state.username,Pool: userPool
});
cognitoUser.authenticateUser(authDetails,{
  onSuccess: (result) => {
    console.log(`access token = ${result.getAccessToken().getJwtToken()}`);
  },onFailure: (err) => {
    alert(err);
  }
});

CognitoIdentityServiceProvider是aws-sdk的一部分,而不是amazon-cognito-identity-js库.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读