AWS Cognito 服务 API?Amplify => Javascript SDK Angular app



我使用Amplify进行身份验证,它似乎运行良好。现在我想用用户池为CRUD设置一个管理应用程序。我似乎不得不离开Amplify,使用JavaScript SDK来使用适当的api。

这是如何工作的我没能弄清楚如何将我在Amplify中收到的代币放入AWS.config或它们应该去的地方。

这真是一场斗争。文档中的代码似乎已经过时,而网上的一些小建议更糟糕。我怀疑这是因为Amplify对象包含配置选项,我必须将这些选项带到AWS.config对象中。我在下面试过,但失败了知道我需要做什么吗我相信这里的答案对许多AWS新手都很有用。

我在我的Angular应用程序中有这段代码,但正在考虑Lambda。我有一个EC2服务器,Node.js是另一个选项。

这是为我的MBP开发的,但我正在将其与AWS集成。

使用下面的代码,我得到一条错误消息,其中部分包含:

Error in getCognitoUsers:  Error: Missing credentials in config
at credError (config.js:345)
at getStaticCredentials (config.js:366)
at Config.getCredentials (config.js:375)

我在下面的对象中插入的JWT是浏览器存储中的AccessKeyID,用于身份验证。

在console.log cognitoentityserviceprovider中,我有一个对象,部分是:

config: Config
apiVersion: "2016-04-18"
credentialProvider: null
credentials: "eyJraWQiOiJwaUdRSnc4TWtVSlR...
endpoint: "cognito-idp.us-west-2.amazonaws.com"
region: "us-west-2"
endpoint: Endpoint
host: "cognito-idp.us-west-2.amazonaws.com"
hostname: "cognito-idp.us-west-2.amazonaws.com"
href: "https://cognito-idp.us-west-2.amazonaws.com/"

这些函数按顺序向下流动。我在主体中留下了一些变量,以防有人想知道如何从用户对象中获取这些数据。我在构建对象的各种尝试中都使用了它们,但这里可能不需要大多数。所有这些都会从Amplify用户对象中产生正确的结果。

import { AmplifyService }  from 'aws-amplify-angular';
import Amplify, { Auth  } from 'aws-amplify';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
import * as AWS from 'aws-sdk';
@Injectable()
export class CognitoApisService {
private cognitoConfig = Amplify.Auth.configure();  // Data from main.ts
private cognitoIdPoolID = this.cognitoConfig.identityPoolId;
private cognitoUserPoolClient = this.cognitoConfig.userPoolWebClientId;
private cognitoIdPoolRegion = this.cognitoConfig.region;
private cognitoUserPoolID = this.cognitoConfig.userPoolId;
...
constructor(
private amplifyService: AmplifyService,
) { }
public getAccessToken() {
return this.amplifyService
.auth()  // Calls class that includes currentAuthenticaedUser.
.currentAuthenticatedUser()  // Sets up a promise and gets user session info.
.then(user => {
console.log('user: ', user);
this.accessKeyId = user.signInUserSession.accessToken.jwtToken;

this.buildAWSConfig();
return true;
})
.catch(err => {
console.log('getAccessToken err: ', err);
});
}
public buildAWSConfig() {
// Constructor for the global config.
this.AWSconfig = new AWS.Config({
apiVersion: '2016-04-18',
credentials: this.accessKeyId,
region: this.cognitoIdPoolRegion
});
this.cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider(this.AWSconfig);
/*  This doesn't get creds, probably because of Amplify.
this.cognitoidentityserviceprovider.config.getCredentials(function(err) {
if (err) console.log('No creds: ', err);    // Error: Missing credentials
else console.log("Access Key:", AWS.config.credentials.accessKeyId);
});
*/
console.log('cognitoidentityserviceprovider: ', this.cognitoidentityserviceprovider);
this.getCognitoUsers();
}

public getCognitoUsers() {
// Used for listUsers() below.
const params = {
UserPoolId: this.cognitoUserPoolID,
AttributesToGet: [
'username',
'given_name',
'family_name',
],
Filter: '',
Limit: 10,
PaginationToken: '',
};

this.cognitoidentityserviceprovider.listUsers(params, function (err, data) {
if
(err) console.log('Error in getCognitoUsers: ', err); // an error occurred
else
console.log('all users in service: ', data);
});
}

一个问题是凭据需要来自Amplify的整个用户对象,而不仅仅是我上面显示的访问令牌。顺便说一句,我在main.ts中有Cognito设置。它们也可以在environment.ts中使用。更好的安全选项是将其迁移到服务器端。还不知道该怎么做。

// Constructor for the global config.
this.AWSconfig = new AWS.Config({
apiVersion: '2016-04-18',
credentials: this.accessKeyId, // Won't work.
region: this.cognitoIdPoolRegion
});

我的完整代码更简单了,现在是可观察的。注意到我必须解决的另一个重大问题。从Amplify导入AWS对象,而不是从SDK导入。请参见下文。

是的,这违背了当前的文档和教程。如果你想了解更多关于这件事最近发生了什么变化的背景,即使是在我做这件事的时候,请参阅这个Github问题的底部。Amplify主要用于身份验证,JavaScript SDK用于服务API。

import { AmplifyService }  from 'aws-amplify-angular';
// Import the config object from main.ts but must match Cognito config in AWS console.
import Amplify, { Auth  } from 'aws-amplify';
import { AWS } from '@aws-amplify/core';
import { CognitoIdentityServiceProvider } from 'aws-sdk';
// import * as AWS from 'aws-sdk';  // Don't do this.
@Injectable()
export class CognitoApisService {
private cognitoConfig = Amplify.Auth.configure();  // Data from main.ts
private cognitoIdPoolRegion = this.cognitoConfig.region;
private cognitoUserPoolID = this.cognitoConfig.userPoolId;
private cognitoGroup;
private AWSconfig;
// Used in listUsers() below.
private params = {
AttributesToGet: [
'given_name',
'family_name',
'locale',
'email',
'phone_number'
],
// Filter: '',
UserPoolId: this.cognitoUserPoolID
};

constructor(
private amplifyService: AmplifyService,
) { }
public getCognitoUsers() {
const getUsers$ =  new Observable(observer => {
Auth
.currentCredentials()
.then(user => {
// Constructor for the global config.
this.AWSconfig = new AWS.Config({
apiVersion: '2016-04-18',
credentials: user,    //  The whole user object goes in the config.credentials field!  Key issue.
region: this.cognitoIdPoolRegion
});
const cognitoidentityserviceprovider = new CognitoIdentityServiceProvider(this.AWSconfig);
cognitoidentityserviceprovider.listUsers(this.params, function (err, userData) {
if (err) {
console.log('Error in getCognitoUsers: ', err);
} else {
observer.next(userData);
}
});
});
});
return getUsers$;
}

让我们从组件调用此服务。我把JS对象解析放在组件中,但现在,我把console.log留在这里让您开始,看看代码是否适用于您的应用程序。

// Called from button on html component.
public getAllCognitoUsers() {
this.cognitoApisService.getCognitoUsers()
.subscribe(userData => {
console.log('data in cognito component: ', userData);
})
}

相关内容

  • 没有找到相关文章

最新更新