gpt4 book ai didi

angular - 在 Angular 2 中实现 AWS-Cognito

转载 作者:太空狗 更新时间:2023-10-29 17:43:51 26 4
gpt4 key购买 nike

我目前正在尝试使用 Angular 和 AWS 构建网络应用程序。我的第一步是使用 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
};

但是当我运行代码时,我出现了以下错误:

TypeError: Cannot read property 'AuthenticationDetails' of undefined

我是不是漏掉了一步?在我的 Angular 应用中实现 Cognito SDK 的最佳方式是什么?

谢谢!

最佳答案

你不应该将整个包导入为

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

这是个坏主意,因为您不需要在其中加入一堆臃肿的东西。

而是仅导入您需要的内容。请参阅下面的示例。

import {AuthenticationDetails, CognitoUser, CognitoUserAttribute, CognitoUserPool} from 'amazon-cognito-identity-js';

const PoolData = {
UserPoolId: 'us-east-1-xxxxx',
ClientId: 'xxxxxxxxxxx'
};

const userPool = new CognitoUserPool(PoolData);

/////in export class....

/// Sign Up User
signupUser(user: string, password: string, email: string) {
const dataEmail = {
Name: 'email',
Value: email
};
const emailAtt = [new CognitoUserAttribute(dataEmail)];

userPool.signUp(user, password, emailAtt, null, ((err, result) => {
if (err) {
console.log('There was an error ', err);
} else {
console.log('You have successfully signed up, please confirm your email ')
}
}))
}

/// Confirm User

confirmUser(username: string, code: string) {
const userData = {
Username: username,
Pool: userPool
};

const cognitoUser = new CognitoUser(userData);

cognitoUser.confirmRegistration(code, true, (err, result) => {
if (err) {
console.log('There was an error -> ', err)
} else {
console.log('You have been confirmed ')
}
})
}

//// Sign in User

signinUser(username: string, password: string) {
const authData = {
Username: username,
Password: password
};
const authDetails = new AuthenticationDetails(authData);
const userData = {
Username: username,
Pool: userPool
};
const cognitoUser = new CognitoUser(userData);

cognitoUser.authenticateUser(authDetails, {
onSuccess: (result) => {
// console.log('You are now Logged in');
this.isUser.next(true);
this.router.navigate(['/'])
},
onFailure: (err) => {
console.log('There was an error during login, please try again -> ', err)
}
})
}

/// Log User Out
logoutUser() {
userPool.getCurrentUser().signOut();
this.router.navigate(['home'])
}

关于angular - 在 Angular 2 中实现 AWS-Cognito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43946839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com