简体   繁体   中英

Javascript Facebook SDK: how to really log out?

First of all: I'm aware that there's plenty of questions about the same topic, but none of them did the trick for me (I've already been like 3 days trying to get this working)...

I'm working on a Javascript mobile game which includes some FB functions (through the Javascript Facebook API). I'm having trouble while trying to really log out from Facebook to log in with another user: everytime I log out, I expect to call the log in function and prompt the FB login dialog where I can specify my e-mail and my password , but instead of this, Facebook logs in automatically with the last user without even asking me for anything... I'm using CocoonJS as the mobile platform and plain Javascript (no jQuery):

Log in function:

CocoonJS.Social.Facebook.init({
    appId:<<MYAPPID>>,
    channelUrl: "channel.html"
});

var socialService = CocoonJS.Social.Facebook.getSocialInterface();

socialService.login(function(loggedIn, error) {
    if (error) {
        console.error("login error: " + error.message);
    }else if (loggedIn) {
        console.log("login suceeded");

        // Ask for extended permissions
        CocoonJS.Social.Facebook.requestAdditionalPermissions("publish", "publish_actions", 
            function(response) 
            {
                callback(response.error ? false : true);
            }
        );

        CocoonJS.Social.Facebook.getLoginStatus(function(response) {
            if (response.status === 'connected')
            {
                CocoonJS.Social.Facebook.api(
                    "/me",
                    function (response) {
                        if (response && !response.error) {
                            // Getting "me" returns the information of the "same" user!
                            console.log("User: "+response.first_name+" "+response.last_name);
                        }
                    }
                );
            }
        });
    }
});

Log out function:

CocoonJS.Social.Facebook.getLoginStatus(function(response) {
    if (response.status === 'connected')
    {
        CocoonJS.Social.Facebook.logout(function(response) {
            console.log("User logged out!");
        });
    }
});

The console log is thrown, so it seems that the user has actually logged out, but when I restart the game, a kind of "I'm doing something" screen appears for less than a second and the same user who logged out is logged in again (even throwing their personal information through calling "me")... I was expecting Facebook to ask me with which user I would like to log in to my app, but it doesn't...

I thing it has something to do with "session" or "cookies", but I don't know how to clear them through Javascript (and, as I'm using CocoonJS as the "browser", I have little control over it)... Any idea?

Thanks in advance for your time and effort! :)

CocoonJS uses native iOS and Android Facebook SDKs. The SDK itself takes care of the session storage, no cookies are used in CocoonJS.

Facebook SDK has two different methods to close the session:

  • close() : Closes the local in-memory session object, but does not clear the persisted token cache.
  • closeAndClearTokenInformation() : Closes the in-memory session, and clears any persisted cache related to the session

CocoonJS.Social.Facebook.logout calls internally to closeAndClearTokenInformation on Android and iOS. It erases all the cached tokens, so the next login starts from scratch.

Facebook SDK uses the Facebook Application to handle the login process if it's installed on the device, otherwise it fallbacks to a webview based login. The problem may be that you are testing on a device with the Facebook Application installed, so the SDK is able to get the logged in user from the Facebook App and doesn't need to ask for it again. If you logout from the Facebook Application or you test your app on a device with no Facebook App, Facebook will ask for a user and password.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM