Search code examples
facebookauthenticationfacebook-unity-sdk

Facebook always logins with same user even though I am using different users


I'm developing an Android game in Unity and using Facebook SDK as the authenticator.

After completing the setup phase in Unity I tried to login and it worked. User that I used to login was the admin user which I also used to create the app at developer.facebook.com.

Later I tried to connect with other users. They were real accounts which have roles of developer and tester, I even used the test users that are automaticly created by FB but even though they all have different credentials they all logged as the admin user.

to make it clear I did the following:

1) Run the game on android.

2) Hit the login button and FB app will start to run.

3) Login with admin user.

4) Now I am logged with Admin user.

5) Close the app and logout from FB.

6) Run the app and this time login with user B (Dev, tester, etc.)

7) Instead of user B again admin user will be authenticate.

Edit: Today I upgraded the FB sdk. In first try I logged with a test user and logged in correctly. On later connections I used the Admin and other test users but they all logged as the first test user. I guess problem isn't related with admin user but the one that first login's.

I used friendsmash tutorial as example for my game and below you can find the code piece that I used in my app.

public void Init() {
    FB.Init(SetInit, OnHideUnity);        
}

private void SetInit() {       
    enabled = true;
}

private void OnHideUnity(bool isGameShown) {
    Debug.Log("OnHideUnity");
    if(!isGameShown) {
        // pause the game - we will need to hide
        Time.timeScale = 0;
    }
    else {
        // start the game back up - we're getting focus again
        Time.timeScale = 1;
    }
}

public void FBLogin(LoginOnSuccess cb) {    
    loginOnSuccess = cb;
    FB.Login("user_friends", LoginCallback);        
}

void LoginCallback(FBResult result) {
    if(FB.IsLoggedIn) {            
        FB.API("/me?fields=id,name", Facebook.HttpMethod.GET, delegate (FBResult response) {
            if(response.Error != null) {
                Debug.Log("Error: FB.API: /me?fields=id,name");
            }
            Debug.Log("Permissions: " + response.Text);
        });
    }
    else {
        //TODO login failed. Give a proper error message on gui
        Debug.Log("FacebookController.cs: Couldn't connect to FB.");
    }
}

Solution

  • Ok, I found the problem.

    Logouting from the Android Facebook app doesn't logout you from your game. Also If there is already logged in user (this means FB.IsLoggedIn == true. as you can see from my code I removed this check. Originally this is used in friendsmash tutorial of FB), and you call FB.Login() this doesn't do anything. FB SDK keeps the previous session. I think API has to warn the developer/user that "you are switching user" or "you cant relog while you are allready logged in".

    So what I am doing is, calling FB.Logout() in callback of FB.Init(SetInit, OnHideUnity):

    private void SetInit() {       
        enabled = true;
        FB.Logout();
    }