2016-05-16 97 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

你是我的英雄人 –