Search code examples
javascriptfacebookamazon-web-servicesamazon-cognitoaws-mobilehub

Login in with Facebook but still getting: "Unauthenticated access is not supported for this identity pool"


I am working for a WebApp using AWS. I am trying to get items from my DynamoDB table but I am getting the error "Unauthenticated access is not supported for this identity pool". I don't want my app to have Unauthenticated users, but I AM LOGIN IN before calling the DynamoDB query. Can Anyone help me? Here's my code:

function facebookLogin () {

FB.login(function (response) {
  if (response.authResponse) { // logged in
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myActualPoolId'
    });

    AWS.config.region = 'us-east-1';
    AWS.config.credentials.params.logins = {}
    AWS.config.credentials.params.logins['graph.facebook.com'] = response.authResponse.accessToken;
    AWS.config.credentials.expired = true;



    console.log("Importing drivers into DynamoDB. Please wait.");


    var drivers = JSON.parse('[{"userId": "4","driverId": "4d","ratingValue": 3,"truckId": "4"},{"userId": "5","driverId": "5d","ratingValue": 2,"truckId": "5"}]');
    drivers.forEach(function(driver) {
        var params = {
            TableName: "myActualTableName",
            Item: {
                "userId":  driver.year,
                "driverId": driver.title,
                "ratingValue":  driver.info,
                "truckId": driver.truckId
            }
        };

        var docClient = new AWS.DynamoDB.DocumentClient();
        docClient.put(params, function(err, data) {
           if (err) {
               console.error("Unable to add driver", driver.userId, ". Error JSON:", JSON.stringify(err, null, 2));
           } else {
               console.log("PutItem succeeded:", driver.userId);
           }
        });
    });

  } else {
    console.log('There was a problem logging you in.');
  }
  });
 }

I would appreciate any help. Thanks!


Solution

  • You're very close. Cognito credentials providers get credentials lazily, so while you are setting the logins, you aren't making the call to link the login to the identity, so the call to Dynamo is happening with an unauthenticated id. The Cognito dev guide has specific examples of how to do this, a relevant one is below:

    FB.login(function (response) {
    
    // Check if the user logged in successfully.
    if (response.authResponse) {
    
    console.log('You are now logged in.');
    
    // 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.
    });
    
    } else {
      console.log('There was a problem logging you in.');
    }
    
    });