0

所以我用這個塊的代碼在AWS上JS SDK的官方回購找到。 它用於驗證用戶。AWS Cognito無法驗證:空白答案 - JS SDK

它返回一個空應答。

AWSCognito.config.region = 'us-east-1'; 
var authenticationData = { 
    Username : '+1112223333', //some phone number used as an Alias 
    Password : 'password123456', 
}; 
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
var poolData = { 
    UserPoolId : 'us-east-1_P00l1d', // Your user pool id here 
    ClientId : 'xxx' // Your client id here 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
var userData = { 
    Username : 'myusername', 
    Pool : userPool 
}; 
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 
cognitoUser.authenticateUser(authenticationDetails, { 
    onSuccess: function (result) { 
     console.log('access token + ' + result.getAccessToken().getJwtToken()); 
     AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
      IdentityPoolId : 'xxx', // your identity pool id here 
      Logins : { 
       // Change the key below according to the specific region your user pool is in. 
       'cognito-idp.pool_id_number_here_xxx' : result.getIdToken().getJwtToken() 
      } 
     }); 
    }, 

    onFailure: function(err) { 
     alert(err) 
     console.log("Error " + err); 
    }, 

}); 

因此,對於authenticationData,我使用電話號碼作爲用戶名(電話號碼設置爲別名)和密碼。我也直接用用戶名嘗試。 UserPoolId和ClientId是正確的,因爲我使用了相同的值來註冊和確認電話號碼。 在userData中,我使用正確的myusername設置了用戶名。

關於身份游泳池,我創建了一個身份池上鍊接到我的App和我的了userpool AWS Console和我更換了價值IdentityPoolId和登錄中的authenticateUser。 雖然我不完全確定登錄中的值。我使用了cognito-idp.POOLIDNUMBER。

AWS的輸出爲空白。 我想我甚至無法訪問服務器,我懷疑角色或身份池的問題(userPool很好,我想)。

我的身份池僅使用AWS Cognito用戶,而不是Facebook或其他身份提供者。

回答

0

用--with-all重新編譯SJCL解決了這個問題。

+0

它應該是編輯或評論但不是一個答案 – Deep

0

請確保您擁有'用於JavaScript的Amazon Cognito Identity SDK'的所有必需庫。以下是這些圖書館的清單和鏈接。

1.亞馬遜AWS Cognito SDK爲JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js

2.亞馬遜Cognito身份SDK爲JavaScript - https://raw.githubusercontent.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js

3.JavaScript BN庫(jsbn.js,jsbn2.js) - http://www-cs-students.stanford.edu/~tjw/jsbn/

4.Stanford的JavaScript加密庫 - https://github.com/bitwiseshiftleft/sjcl

5.AWS SDK for JavaScript(可選,使用其他aws服務) - http://aws.amazon.com/sdk-for-browser/

包含所有這些庫,然後使用下面的代碼片段。

AWSCognito.config.region = 'YOUR_REGION'; 
var poolData = { 
    UserPoolId : 'YOUR_USER_POOL_ID', // Your user pool id here 
    ClientId : 'YOUR_CLIENT_ID' // Your client id here 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
var userData = { 
    Username : "userName", // your username here 
    Pool : userPool 
}; 
//Signing Users in the App 
var authenticationData = { 
    Username : "userName", // your username here 
    Password : "password" // your password here 
}; 

var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 

var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData); 

cognitoUser.authenticateUser(authenticationDetails, { 
    onSuccess: function (result) { 
     console.log('access token + ' + result.getAccessToken().getJwtToken()); 
    }, 
    onFailure: function(err) { 
     console.log(err); 
    } 
}); 
+0

它有幫助...? –