Reputation: 111
I established a authentication flow with Facebook Login and AWS Cognito on the client. I need a reference of the user with its facebook id in a dynambodb table and verify that the API call actually has a valid facebook id this facebook id matches the AWS Cognito Id. I found a solution in this answer: AWS Cognito, Lambda, User credentials in DynamoDB
But after I login in the Cognito for several times on the same device , I found that there are several different identity id in the console. Like this: screen shot
I thought the identity id would be unique for a certain device with a certain Facebook id. Do I make some mistake? If the identity id changes, how can I store the data of the Facebook user in dynamoDB?
This is my code:
void Start()
{
InitCognito ();
}
public void InitCognito()
{
UnityInitializer.AttachToGameObject (this.gameObject);
credentials = new CognitoAWSCredentials (
identity_pool_id, // Identity Pool ID
region // Region
);
Debug.Log ("identity_pool_id = " + identity_pool_id + " region = " + region);
credentials.GetIdentityIdAsync(delegate(AmazonCognitoIdentityResult<string>
result) {
if (result.Exception != null) {
Debug.LogError(result.Exception.ToString());
}
string identityId = result.Response;
Debug.Log("identityId = "+identityId);
FBInit();
});
}
public void FBInit()
{
FB.Init(this.OnInitComplete, this.OnHideUnity);
Debug.Log( "FB.Init() called with " + FB.AppId);
}
public void FBLogin()
{
FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, this.HandleResult);
}
private void OnInitComplete()
{
Debug.Log( "Success - Check log for details");
Debug.Log("Success Response: OnInitComplete Called\n");
Debug.Log( string.Format(
"OnInitCompleteCalled IsLoggedIn='{0}' IsInitialized='{1}'",
FB.IsLoggedIn,
FB.IsInitialized));
if (AccessToken.CurrentAccessToken != null)
{
Debug.Log("Access token = "+AccessToken.CurrentAccessToken.ToString());
}
FBLogin ();
}
private void OnHideUnity(bool isGameShown)
{
Debug.Log( "Success - Check log for details");
Debug.Log(string.Format("Success Response: OnHideUnity Called {0}\n", isGameShown));
Debug.Log("Is game shown: " + isGameShown);
}
protected void HandleResult(IResult result)
{
if (result == null)
{
Debug.Log("Null Response\n");
return;
}
// Some platforms return the empty string instead of null.
if (!string.IsNullOrEmpty(result.Error))
{
Debug.Log( "Error - Check log for details");
Debug.Log( "Error Response:\n" + result.Error);
}
else if (result.Cancelled)
{
Debug.Log ("Cancelled - Check log for details");
Debug.Log( "Cancelled Response:\n" + result.RawResult);
}
else if (!string.IsNullOrEmpty(result.RawResult))
{
Debug.Log ("Success - Check log for details");
Debug.Log ("Success Response:\n" + result.RawResult);
Debug.Log ("Access Token = "+AccessToken.CurrentAccessToken);
Debug.Log ("Access Token = "+AccessToken.CurrentAccessToken.TokenString);
Debug.Log ("Access User Id =" + AccessToken.CurrentAccessToken.UserId);
credentials.AddLogin ("graph.facebook.com", AccessToken.CurrentAccessToken.TokenString);
if (credentials.CurrentLoginProviders.Length > 0) {
Debug.Log (credentials.CurrentLoginProviders[0]);
}
Debug.Log (credentials.GetCachedIdentityId());
}
else
{
Debug.Log ( "Empty Response\n");
}
}
When the InitCognito() method is executed, I get a unAuthorized Identity Id(once I reinstall this app on the same device, the unAuthorized Identity Id changes). The I can get the Facebook user id and token successfully. After the credentials.AddLogin() method executed, Debug.Log (credentials.GetCachedIdentityId()) shows that the Identity Id is the same as the unAuthorized Identity Id, not a specific id referenced to the Facebook Id. Do I use credentials.AddLogin() in a wrong way?
Thanks!
Upvotes: 0
Views: 1507
Reputation: 5775
The reason you have multiple identities showing up is they're unauthenticated. You're making lots of requests without giving the facebook token. Any requests with the facebook token will give you the same authenticated id (the one you mentioned above), but you do have to continue providing that token to get it. Those will map to a unique id, but requests without one will not.
Upvotes: 2