Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set a callback when a user cancels a login request #288

Closed
rbahumi opened this issue May 30, 2013 · 7 comments
Closed

How to set a callback when a user cancels a login request #288

rbahumi opened this issue May 30, 2013 · 7 comments

Comments

@rbahumi
Copy link

rbahumi commented May 30, 2013

Hi,

The FB.login() in Facebook's Javascript SDK also detects when a user cancels a login request. My login callback is only being called when a user confirm the permissions, never when he/she cancels the login request.

Here is and example from Facebook's documentation:

 FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 });

I am asking this because I am adding a loading spinner to the login button, and the spinner should be removed when either, a successful login occurs or a user cancels the login request.

Can this behavior also be achieved with this plugin?

Roei

@scyclops
Copy link

I'd also like to have this behavior available.

@rbahumi
Copy link
Author

rbahumi commented May 30, 2013

I have found a workaround for this....
I don't treat the FB.login() function as the actual login function... Instead, I treat calls to FB.api('/me' ...) as my actual login function.

In my scenario, it is kind of equivalent. Hope that helps.

@sebastianzillessen
Copy link
Contributor

How do you do this then? I really need this feature as well. A loading spinner is needed in my application but the app does not receive a callback on iOS when the user cancels the permission request.

I hope someone can help me with this.

Thanks!

@jakeonrails
Copy link

👍 this is really important!

@jakeonrails
Copy link

In the cordova facebook javascript there is this replacement for the fb login method:

  login: function(params, cb, fail) {
    params = params || { scope: '' };
    cordova.exec(function(e) { // login
        if (e.authResponse && e.authResponse.expiresIn) {
          var expirationTime = e.authResponse.expiresIn === 0
          ? 0
          : (new Date()).getTime() + e.authResponse.expiresIn * 1000;
          e.authResponse.expirationTime = expirationTime;
        }
        localStorage.setItem('cdv_fb_session', JSON.stringify(e.authResponse));
        FB.Auth.setAuthResponse(e.authResponse, 'connected');
        if (cb) cb(e);
    }, (fail?fail:null), 'org.apache.cordova.facebook.Connect', 'login', params.scope.split(',') );

You can see that it simply calls the failure callback, and doesn't do anything to actually call the cb method (your call back function).

However, if you replace the fail method, it seems like it still doesn't fire (at least on iOS).

So from what i can tell this is either a failure on the part of cordova.exec, or somehow the Connect plugin itself is barfing on us.

However, there is a "hackish" workaround that I've just figured out.

The Workaround

In your client FB code, you can do this:

  var loggedIn = false;
  FB.login(function(e) {
    if (e.authResponse) {
      setTimeout(function() {
        loggedIn = true;
      }, 0);
      alert('user logged in');
    else { 
      // failure, but no handler unforunately
    }
  setTimeout(function() {
    if (!loggedIn) {
      alert("Not logged in");
    }
  }, 1000);

If you don't use the setTimeouts, the not logged in code will execute before your logged in code when the user does approve the app. The second setTimeout may not need to be a whole second but it works for me.

@rbahumi
Copy link
Author

rbahumi commented Jun 27, 2013

The below code is the solution I used in my application. Hopefully, you'll find that helpful:

FB.init({
    appId: Surfspace.app.APP_ID,
    nativeInterface: CDV.FB,
    useCachedDialogs: false
});

FB.getLoginStatus(handleStatusChange);
FB.Event.subscribe('auth.statusChange', handleStatusChange);


// This is the callback function for your login button
function onLoginButtonPress(){
    var FB = window.FB;
    FB.login(function(result){}, {scope: "email,user_about_me"} );
}


//This is the function that handles the more-verbose 'result' object
function handleStatusChange(session){
    if (session.authResponse) {
        showLoginLoader(); //here you show tour loading spinner
         FB.api('/me', {fields: 'name, id, username, email, gender'}, function(result) {
            console.log("FB.api('/me'):" + JSON.stringify(result));

            // Check for cancellation/error
            if(result.cancelled || result.error) {
                console.log("FacebookConnect.login:failedWithError:" + result.message);
                removeLoginLoader(); //remove the 
                return false;
            }

            // Success - do your thing here.
            removeLoginLoader();
        });
    }
}

@aogilvie
Copy link
Collaborator

Closing. Out-of-date.

slachkar pushed a commit to PopSugar/phonegap-facebook-plugin that referenced this issue Aug 30, 2016
Expose the "quote" property on share dialogs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants