0

I logged in to facebook with a user on my app.

Later when I log out manually this user on my Facebook app and on the browser,

(FB.IsLoggedIn) still returns true. For some reason, the old profile is saved and I can't login with a new user

Here is my code:

public void FacebookLogin()
{
    if (FB.IsLoggedIn)
        FB.LogOut();  //it doesn't work, user is still logged in


     var permissions = new List<string>() {"email"};
      FB.LogInWithReadPermissions(permissions);  //trying to login a new user, but the last user is still logged in
SHAI
  • 789
  • 3
  • 10
  • 43

2 Answers2

0

Change your FacebookLogin function to a coroutine function. By doing this, you can check if the user is logged in, log the user out then wait every frame until FB.IsLoggedIn is false before you can login another user. Also add a timer to the wait so that when FB.IsLoggedIn is never false within x amount of time, the function won't continue to run but show some error and exit.

public IEnumerator FacebookLogin()
{
    //5 seconds loggout time
    float waitTimeOut = 5f;

    //Log out if loggedin
    if (FB.IsLoggedIn)
        FB.LogOut();  //it doesn't work, user is still logged in

    //Wait until logout is done. Also add a timeout to the wait so that it doesnt wait forever
    float timer = 0;
    while (FB.IsLoggedIn)
    {
        if (timer > waitTimeOut)
        {
            Debug.LogError("Failed to log out within " + waitTimeOut + " seconds");
            yield break;
        }
        timer += Time.deltaTime;
        yield return null;
    }
    Debug.Log("Successfully logged out. Now logging another user in");
    var permissions = new List<string>() { "email" };
    FB.LogInWithReadPermissions(permissions);  //trying
}

If you still have issue with this, you need to file for a bug report on the facebook-sdk-for-unity Github page with the code in this answer.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • And you have to start it not call it since it's a coroutine function. `StartCoroutine(FacebookLogin());` – Programmer Oct 25 '18 at 23:08
  • I'm not sure if that's the right solution for switching users. 1. Do I really need to use `FB.LogOut()` every time I log in? (in my example I used it because the last user stays logged in even though I logout it manually from the facebook app and logged in a new user). Also, by reading the documentation- `FB.IsLoggedIn` would be true immediately after calling `FB.Logout()`. so the while loop isn't safe – SHAI Oct 26 '18 at 22:07
  • For 23 hours I was waiting to hear if the code works or not but only got *"not sure this is the right to do it"* comment. **1.** You can't login to any website I know with multiple account at **the-same** time. You must logout from one account before login in from another one. **2.** The `FB.LogOut` function in my answer will only be called if a user is logged in. **3.** It did not mention any in the doc that `FB.IsLoggedIn` would be true immediately after calling `FB.Logout()`. Provided a link to prove so. **4.** Do you mind explaining why the `while` loop isn't safe? – Programmer Oct 26 '18 at 22:23
  • Hey sorry! just sharing my thoughts with you- no need to get upset. And it was based on comments for this post: https://stackoverflow.com/questions/23244611/fb-logout-not-working-in-facebook-unitysdk – SHAI Oct 26 '18 at 23:05
  • I did test your code yesterday- and it gave me an error. I didn't want to bug you with it because I wanted to solve it myself TODAY. Now, the error is because after `LogInWithReadPermissions` in my original code- I print the user details. But it doesn't print it because we must add to the end of the function WaitForSeconds, so we make sure we're logged in before trying anything. – SHAI Oct 26 '18 at 23:07
0

You are logging in after you log out.

public void FacebookLogin()
    {
            if (FB.IsLoggedIn)
             {   
              FB.LogOut();  //it doesn't work, user is still logged in
              return;
             }
              var permissions = new List<string>() {"email"};
              FB.LogInWithReadPermissions(permissions);  //t
    }
GTAWWEKID
  • 11
  • 6