Facebook access token is null on 2nd attempt of log in with facebook using ParseFacebookUtils

Viewed 481

I am using parse server as a backend in the app. I've the requirement to login with Facebook as a sign up option. For that I am using

compile 'com.parse:parsefacebookutils-v4-android:1.10.3@aar'

Using this latest Facebook sdk for Android

compile('com.facebook.android:facebook-android-sdk:4.26.0') {
        exclude group: 'com.parse.bolts',
                module: 'bolts-android'
    }

When login first time after installing the app with facebook it all works fine

private void loginWithFacebook(){
        ParseFacebookUtils.logInWithReadPermissionsInBackground(this, Arrays.asList("public_profile", "email", "user_friends"), new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException err) {
                //....
            }
        });
    }

I can have the access token with other information which i can use later on in the app for different purpose, but the problem starts when i logout using:

LoginManager.getInstance().logOut();

this function logs me out properly but after logging again with same Facebook account using same logic for login when i debug and tries to get the Access Token it's null which I am not able to understand why as it should work just fine.

The account that I am using to login is already a test account which works fine with the iOS version of the application with the same flow but not in Android.

Let me know if I am doing anything wrong.

1 Answers

Try to log out using this method:

ParseUser.logOut()

public static void logOut()

Logs out the currently logged in user session. This will remove the session from disk, log out of linked services, and future calls to ParseUser.getCurrentUser() will return null. Typically, you should use ParseUser.logOutInBackground() instead of this, unless you are managing your own threading.

Note:: Any errors in the log out flow will be swallowed due to backward-compatibility reasons. Please use ParseUser.logOutInBackground() if you'd wish to handle them.

I think your issue occurs because you are directly using LoginManager.getInstance().logOut() method of Facebook SDK, but ParseFacebookUtils caches some info about previous login. Try to call:

ParseUser.getCurrentUser().logOut();

BTW, if you look into the code of LoginManager.getInstance().logOut(), you will see next:

/**
 * Logs out the user.
 */
public void logOut() {
    AccessToken.setCurrentAccessToken(null);
    Profile.setCurrentProfile(null);
    setExpressLoginStatus(false);
}

It sets Access Token to NULL.

Also people advise to remove next call from your app's Application class:

ParseUser.enableAutomaticUser();
Related