AWS Cognito 服务 API?放大 => Javascript SDK Angular 应用程序
AWS Cognito services APIs? Amplify => Javascript SDK Angular app
我使用 Amplify 进行身份验证,它似乎工作正常。现在我想使用用户池为 CRUD 设置一个管理应用程序。看来我必须离开 Amplify 并使用 JavaScript SDK 来使用适当的 api。
这是如何工作的? 我没弄清楚如何将我在 Amplify 中收到的令牌放入 AWS.config 或它们应该去的任何地方。
这是一场怎样的斗争啊。文档中的代码似乎已过时,网上的一些小建议更糟糕。我怀疑这是因为 Amplify 对象包含配置选项,我必须将它们带到 AWS.config 对象。我在下面尝试过但失败了。 知道我需要做什么吗?我相信这里的答案对许多 AWS 新手很有用。
我的 Angular 应用程序中有此代码,但正在考虑 Lambda。我有一个带有 Node.js 作为另一个选项的 EC2 服务器。
这是为我的 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 cognitoidentityserviceprovider 我有这个对象,部分:
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 issue 的底部。 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);
})
}
我使用 Amplify 进行身份验证,它似乎工作正常。现在我想使用用户池为 CRUD 设置一个管理应用程序。看来我必须离开 Amplify 并使用 JavaScript SDK 来使用适当的 api。
这是如何工作的? 我没弄清楚如何将我在 Amplify 中收到的令牌放入 AWS.config 或它们应该去的任何地方。
这是一场怎样的斗争啊。文档中的代码似乎已过时,网上的一些小建议更糟糕。我怀疑这是因为 Amplify 对象包含配置选项,我必须将它们带到 AWS.config 对象。我在下面尝试过但失败了。 知道我需要做什么吗?我相信这里的答案对许多 AWS 新手很有用。
我的 Angular 应用程序中有此代码,但正在考虑 Lambda。我有一个带有 Node.js 作为另一个选项的 EC2 服务器。
这是为我的 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 cognitoidentityserviceprovider 我有这个对象,部分:
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 issue 的底部。 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);
})
}