Switching Users on nativescript-facebook


#1

I’m utilizing the nativescript-facebook plugin for my app, and I’m able to get all of the pertinent data. However, the issue comes when I try to switch Facebook accounts to login with; when attempting to log in after the first successful log in, it will remember the last account that was used and prompt the user to either continue (which initiates the login process) or cancel (which cancels the login process), leaving out an option to switch users. I saw a post here with the solution of adding a parameter to the init function, but I haven’t been able to get it to work. It kept throwing an error, but my code was as follows:

let nsFacebook = require('nativescript-facebook');
import * as application from 'application';

application.on(application.launchEvent, function(args) {
    nsFacebook.init("fb_id", LoginBehavior.LoginBehaviorWeb);
});

Has anyone else used this plugin and come across this problem? If so, how did you solve it?


#2

What is the error it’s throwing?


#3

Hi @manojdcoder, it’s throwing the error error TS2304: Cannot find name 'LoginBehavior'. Adding declare var LoginBehavior: any; before that raises the error ReferenceError: LoginBehavior is not defined.


#4

Of course LoginBehavior is a enum defined in plugin. You must import it as,

import { LoginBehavior } from "nativescript-facebook";

#5

Thanks @manojdcoder, that fixed the error issue. It now works fine on iOS, but I’m still having an issue switching users on Android; now, when I login with Facebook, it does not give me any dialog pertaining to logging in with Facebook and instead automatically executes the login functions even though I’m logging out using the Facebook widget function. My logout code is as follows:

public logout() {
    ApplicationSettings.clear();
    Facebook.logout(() => {
      console.log("logged out");
    })
    this.router.navigate(["/login"], { clearHistory: true });
}

And my login code is as such:

public loginFacebook() {
    var that = this;
    const readPermissions = ["email", "public_profile"]
    Facebook.requestReadPermissions(readPermissions, (error, fbData) => {
      if (error) {
        alert("Error during login: " + error.message);
      } else {
        getFacebookID(fbData.token)
        .then(function(response) {
          // do stuff with facebook data
          loginOrRegisterFBAccount(response, that, fbData.token) 
        })
      }
    })
  } 

I’m going to try experimenting with the other login behaviors (I see that there’s “LoginBehaviorBrowser” and “LoginBehaviorSystemAccount” in the node_module folder), but I don’t know why it’s not working on Android and, in the original issue I referenced, the original poster said it was a problem specific to iOS and not Android. Do you have any ideas?


#6

I haven’t played much with Facebook in recent days, but manually setting the access token to null in iOS seems promising.

declare let FBSDKAccessToken: any;

import { isIOS } from "platform";
...
...
Facebook.logout(() => {
    if (isIOS) {
      FBSDKAccessToken.setCurrentAccessToken(null);
   }
});

#7

@manojdcoder thanks for the tip. I tried implementing that for Android since it works fine on iOS, but it throws the error FBSDKAccessToken is not defined even when I try importing it from “nativescript-facebook” like with LoginBehavior. Does that code only work with iOS?


#8

Obviously it won’t work for Android as it was iOS specific code. Your original question has a link to issue that discusses problem with iOS (not Android).

You may give this a try for Android.

declare let com: any;

import * as application from "application";
import { isAndroid } from "platform";
...
...
Facebook.logout(() => {
    if (isAndroid) {
      com.facebook.FacebookSdk.sdkInitialize(application.android.context.getApplicationContext());
   }
});

#9

Sorry for the confusion, the post I linked does specify that it is iOS-specific but my post containing my login and logout code discussed having the problem on Android (which was edited for clarity after the fact, which may have lead to the confusion). I implemented the logout code as you suggested, but it also doesn’t seem to work; upon attempting to log in, the outline of a window appears before disappearing and continuing to automatically continue the login process with the previous user. Regardless, I appreciate your continued help and your patience.