2016-12-06 54 views
3

在瀏覽器中,在Facebook登錄後,調用statusChangeCallback。一切都成功了。 Cognito甚至返回一個身份標識。但是,userPool.getCurrentUser()返回null。 Cognito認爲沒有經過認證的用戶。我該如何解決這個問題?謝謝。AWS Cognito通過Facebook驗證成功,但getCurrentUser返回null

function statusChangeCallback(response) { 
    if(response.status == 'connected' && response.authResponse) { 
     testAPI() 

     console.log("FB statusChangeCallback", JSON.stringify(response)) 

     AWSCognito.config.credentials = new AWSCognito.CognitoIdentityCredentials({ 
      IdentityPoolId : '<%=process.env.AWS_USERPOOLGUID%>', // your identity pool id here 
      Logins : { 
       'graph.facebook.com': response.authResponse.accessToken 
      } 
     }); 
     console.log(JSON.stringify(AWSCognito.config.credentials)) 


     AWSCognito.config.region = '<%= process.env.AWS_REGION%>' 

     AWSCognito.config.credentials.refresh(function(error) { 
      if (error) { 
       console.error("AWSCognito.config.credentials.get", error); 
      } else { 
       console.log("Cognito Identity Id", AWSCognito.config.credentials.identityId); 
       console.log('Successfully logged!'); 
       var cognitoUser = userPool.getCurrentUser(); 
       console.log('cognitoUser', cognitoUser); 

      } 
     }); 
    } 
} 

回答

0
userPool.getCurrentUser(); 

指與關於特定用戶羣的身份驗證的用戶。在上面的代碼中,您正在做的是使用Facebook身份獲取AWS憑證。但是,當前用戶引用用戶池的最後一個已認證用戶。驗證成功後,它將保存在本地存儲中。所以你需要首先進行認證,類似於下面的代碼。

var authenticationData = { 
    Username : 'username', 
    Password : 'password', 
}; 
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData); 
var poolData = { 
    UserPoolId : '...', // Your user pool id here 
    ClientId : '...' // Your client id here 
}; 
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData); 
var userData = { 
    Username : 'username', 
    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 : '...', // your identity pool id here 
      Logins : { 
       // Change the key below according to the specific region your user pool is in. 
       'cognito-idp.<region>.amazonaws.com/<YOUR_USER_POOL_ID>' : result.getIdToken().getJwtToken() 
      } 
     }); 

     // Instantiate aws sdk service objects now that the credentials have been updated. 
     // example: var s3 = new AWS.S3(); 

    }, 

    onFailure: function(err) { 
     alert(err); 
    }, 

}); 
+2

這並不能解釋如何在點擊Facebook登錄按鈕後保護已驗證的會話和用戶,而是解釋了用戶名和密碼組合登錄。 –

0

看起來你需要改變你的AWSCognito.config.credentials 從你有這樣的:

// Add the Facebook access token to the Cognito credentials login map. 
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 
    IdentityPoolId: 'IDENTITY_POOL_ID', 
    Logins: { 
    'graph.facebook.com': response.authResponse.accessToken 
    } 
}); 

// Obtain AWS credentials 
AWS.config.credentials.get(function(){ 
    // Access AWS resources here. 
}); 

注意IdentityPoolId: 'IDENTITY_POOL_ID',而不是IdentityPoolId:'<%= process.env.AWS_USERPOOLGUID%>',//您的身份池ID在這裏

看起來您正嘗試訪問您的用戶池而不是您的IDENTITY POOL

Facebook用戶住在身份池中,因爲他們是來自Facebook服務器的聯盟用戶。

+0

此外,AWS.config.credentials.get與AWS.config.credentials.refresh類似 –