2017-05-20 67 views
0

我試圖讓AWS docsCognito:如何正確地實現增強簡化認證流程

enter image description here

問題描述的增強簡化認證流程,我可以不知道如何正確使用SDK。 ..

AWS.config.region = "ap-northeast-2" 
    const cognitoParams = { 
    IdentityPoolId: "ap-northeast-2:...", 
    Logins: { 
     "accounts.google.com": googleUser.getAuthResponse().id_token 
    } 
    } 
    AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams) 

    const identity = new AWS.CognitoIdentity() 
    identity.getId(cognitoParams, function (err, identityId) { 
    console.log(identityId) 

    const identityParams = Object.assign({}, cognitoParams, { 
     IdentityId: identityId 
    }) 

    identity.getCredentialsForIdentity(identityParams, function (err, data) { 
     console.log(data) 
    }) 
    }) 

的2 console.log給出null

AWS.config.region = "ap-northeast-2" 
const cognitoParams = { 
    IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745", 
    Logins: { 
    "accounts.google.com": googleUser.getAuthResponse().id_token 
    } 
} 
AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams) 

AWS.config.credentials.getId(function (err, identityId) { 
    console.log(identityId) 

    const identityParams = Object.assign({}, cognitoParams, { 
    IdentityId: identityId 
    }) 

    AWS.config.credentials.getCredentialsForIdentity(identityParams, function (err, data) { 
    console.log(data) 
    }) 
}) 

上面給我的身份但失敗Cannot read property 'getCredentialsForIdentity' of undefined

我該如何實施?

回答

0

我發現下面的工作...我應該從CognitoIdentity的實例調用函數,而不是CognitoIdentityCredentials ......但是它在文檔中並不清楚。

實際上它使用CognitoIdentityCredentials和原因呢?我什麼時候使用?

AWS.config.region = "ap-northeast-2" 
    const cognitoParams = { 
    IdentityPoolId: "ap-northeast-2:31cc246c-bd2e-46ee-91da-2b8eefcf0745", 
    Logins: { 
     "accounts.google.com": googleUser.getAuthResponse().id_token 
    } 
    } 
    // AWS.config.credentials = new AWS.CognitoIdentityCredentials(cognitoParams) 

    const identity = new AWS.CognitoIdentity() 
    identity.getId(cognitoParams, function (err, identityData) { 
    if (err) { 
     return console.error(err) 
    } 

    const identityParams = { 
     IdentityId: identityData.IdentityId, 
     Logins: cognitoParams.Logins 
    } 

    identity.getCredentialsForIdentity(identityParams, function (err, data) { 
     if (err) { 
     return console.error(err) 
     } 
     console.log(data) 
    }) 
+0

googleUser是什麼? –