Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to logout with react-native-fbsdk

Tags:

I am using react-native-fbsdk to login through facebook on my react-native app.
I call LoginManager.logOut() to logout: it does not actually properly logout since the next time I try to login, it does not ask me for login/password again so I can only login on one account. I can not find a way to login to another facebook account.

This guy (react-native-fbsdk: How properly log out from facebook?) had the same problem and seem to have found no solution.

One trick on iOS is to go to safari then logout from the mobile facebook website. This does not work on android though :(

EDIT:

Here is my facebook login code:

function login() {
  return LoginManager.logInWithReadPermissions(FACEBOOK_PERMISSIONS)
    .then(result => {
      if (result.isCancelled) {
        throw new Error("Login canceled");
      }
      return AccessToken.getCurrentAccessToken();
    })
    .then(({ accessToken }) => accessToken);
}

Video of logout/login: https://d3vv6lp55qjaqc.cloudfront.net/items/132L2U1p383E1y0l2l2v/Screen%20Recording%202018-10-31%20at%2002.52%20PM.mov

like image 757
httpete Avatar asked Jul 29 '18 21:07

httpete


2 Answers

So I found this solution, that is not a hack but the proper way to perform a logout on facebook. You need to create a GraphRequest to ask a deletion of permissions. Below the code, I hope that will help you. I test it on Android and IOS, and that work like a charm.

    FBLogout = (accessToken) => {
        let logout =
            new GraphRequest(
                "me/permissions/",
                {
                    accessToken: accessToken,
                    httpMethod: 'DELETE'
                },
                (error, result) => {
                    if (error) {
                        console.log('Error fetching data: ' + error.toString());
                    } else {
                        LoginManager.logOut();
                    }
                });
        new GraphRequestManager().addRequest(logout).start();
    };
like image 183
zagoa Avatar answered Sep 28 '22 17:09

zagoa


The problem is not with react-native-fbsdk but with Facebook or the browser through which the user logs in to connect to your app, the reason being every-time your app accesses the Facebook login through the browser or the Facebook app where the user-account is already logged-in, which is why it doesn't show you username or password fields.

To solve this issue, the user must logout from the browser or Facebook through which he/she logged in (for app permission initially) to your app, so when the user comes back to your app and selects the Facebook-login option, assuming user logged-out of your app as well, then he/she can see it redirecting to the Facebook or browser login page with username and password fields.

like image 38
Vidya Sairam Avatar answered Sep 28 '22 19:09

Vidya Sairam