2016-05-16 52 views
6

我正尝试在this教程的帮助下创建Web App身份验证。这是我写的代码 -亚马逊Cognito的用户池 - CredentialsError:配置中缺少凭据

var app = {}; 

app.configureCognito = function(){ 
    AWSCognito.config.region = 'us-east-1'; 

    var poolData = { 
     UserPoolId: 'userPoolId', 
     ClientId: 'ClientId' 
    }; 

    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
    var userData = { 
        UserName:'MyName', 
        Pool: userPool 
    }; 

    var attributeList = []; 

    var dataEmail = { 
      Name: 'email', 
      Value: '[email protected]' 
    }; 

    var dataPhoneNumber = { 
      Name: 'phone_number', 
      Value: '+9112212212212' 
    }; 

    var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail); 
    var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber); 

    attributeList.push(attributeEmail); 
    attributeList.push(attributePhoneNumber); 

    var cognitoUser; 

    userPool.signUp('userName','password',attributeList,null,function(err,result){ 
      if(err){ 
        console.log(err); 
        alert(err); 
        return; 
      } 
      cognitoUser = result.user; 
      console.log('User Name is: '+cognitoUser); 
    }); 

}; 

我得到它说“在配置缺少凭据”,据我所知,我们没有做任何AWSCognito.config.credentials分配这里,但它不是应该的错误使用userPool对象中提供的信息?代码有什么不对或缺什么?根据UserPoolId和客户端ID,两者都是100%正确的。任何帮助,将不胜感激。

回答

9

我把它用下面的代码显然,有没有必要在这个例子中提供IdentityPoolId都解决了,这些只是可以留给下面的占位符 -

AWS.config.region = 'us-east-1'; // Region 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: '...' 
}); 

AWSCognito.config.region = 'us-east-1'; 
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: '...' 
}); 

而且两者的凭证需要设置AWS.config.credentials和AWSCognito.config.credentials。一旦AWSCognito.config需要完成以上步骤如下更新 -

// Need to provide placeholder keys unless unauthorised user access is enabled for user pool 
AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'}) 

var poolData = { 
    UserPoolId : 'user pool id collected from user pool', 
    ClientId : 'application client id of app subscribed to user pool' 
}; 

dataPhoneNumber和用户数据是可选的,dataPhoneNumber应在案件的短信验证需要注册提供。

一旦上面的问题得到解决,Identity-Code是一个工作模式,如果有人想看看它。

+0

哇,非常感谢!也解决了我的问题。这个文档太糟糕了。你从哪里找到这些信息? – user2997154

+2

这是一个打击和审判的不懈努力这里是完整的代码,如果你想寻找参考 - https://github.com/jeetendra-choudhary/Identity-Code.git – Jeet

+0

你是我的英雄人 –