0

I have a simple facebook validation i would like to implement to my jQuery button.
When the user clicks the button it should check if logged in, if TRUE then change the text.
I found this article which talks about returning a true/false state but when i tried to implement it in my code it didnt work.

Any suggestion where im going wrong please, thank you.

function fb_loginCheck(){
    FB.getLoginStatus(function(response, e) {
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            e.returnValue = true;
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, but has not authenticated your app
            fb_oAuth();
            e.returnValue = false;
        } else {
            // the user isn't logged in to Facebook.
            fb_oAuth();
            e.returnValue = false;
        }
    }, true);
}


$('.myBttn').click(function(){

    var io = return fb_loginCheck();
    if (io){
        $this = $(this).text();
        if($this == 'yes')
            $(this).text('no');
        else
            $(this).text('yes');
    }

    return false;
});

got it to work:
similar to potench answer but removing the e.returnValue did it

function fb_loginCheck(callBack){
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            callBack(true);
        } else if (response.status === 'not_authorized') {
            fb_oAuth();
            callBack(false);
        } else {
            fb_oAuth();
            callBack(false);
        }
    }, true);
}
Community
  • 1
  • 1
t q
  • 4,593
  • 8
  • 56
  • 91
  • 2
    `FB.getLoginStatus` does not `return` a value, it's a "delegated" call. So you would need to restructure your code to wait for the facebook response before changing the login-text. – potench Oct 09 '12 at 16:29
  • @potench do you think i should be checking my button click against this? `response.status === 'connected'` – t q Oct 09 '12 at 16:32
  • 2
    potench is right. Also, nothing is executed after `var io = return fb_loginCheck();`, since you're telling the function to `return`. – bfavaretto Oct 09 '12 at 16:32

1 Answers1

1

Something like this might work. I've moved the methods around so they get fired when a response is returned from the FB.getLoginStatus method.

I'm passing in a callBack method which fires when the response from FB.getLoginStatus returns a result. Also note that I've had to re-scope the $(this) variable.

function fb_loginCheck(callBack){
    FB.getLoginStatus(function(response, e) {
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
            e.returnValue = true;
            callBack(true);
        } else if (response.status === 'not_authorized') {
            // the user is logged in to Facebook, but has not authenticated your app
            fb_oAuth();
            e.returnValue = false;
            callBack(false);
        } else {
            // the user isn't logged in to Facebook.
            fb_oAuth();
            e.returnValue = false;
            callBack(false);
        }
    }, true);
}


$('.myBttn').click(function(){
    var targ = $(this);

    fb_loginCheck(function (io) {
        targ.text( (io) ? "yes" : "no" );
    });

    return false;
});
potench
  • 3,802
  • 1
  • 28
  • 39
  • Updated this as `io` should be moved inside your callback. The anonymous function inside `fb_loginCheck` won't fire until you've gotten a response from Facebook. This isn't the most clear way to structure what's going on, but it's the closest to your original code. Let me know if you need something more clearly outlined – potench Oct 09 '12 at 16:41
  • thank you, i 2x checked my code but im not getting a response from the button click – t q Oct 09 '12 at 16:48
  • it seems like everything inside the anonymous function is not being called – t q Oct 09 '12 at 16:57
  • i followed http://stackoverflow.com/questions/5189203/fb-getloginstatus-not-calling-its-callback and made sure i was logged in as administrator to the app – t q Oct 09 '12 at 17:29
  • Is `fb_loginCheck ` firing, is `FB.getLoginStatus` firing, what `response` is `FB.getLoginStatus` returning? Can you provide some more details or better, a JSFiddle? – potench Oct 09 '12 at 17:51
  • everything is firing, i placed `$('body').prepend(accessToken);` inside `if (response.status === 'connected')` and it output a string. i think its inside `fb_loginCheck(function (io)` that is not responding because when i do this `fb_loginCheck(function (io) { alert(1); /* if (io){ //alert(1); $this = targ.text(); if($this == 'Follow') targ.text('Un-Follow'); else targ.text('Follow'); } */ });` nothing happens – t q Oct 09 '12 at 18:16
  • try `FB.getLoginStatus(function(response, e) { callBack(response.status === 'connected'); }, true);` – potench Oct 09 '12 at 18:33
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17768/discussion-between-t-q-and-potench) – t q Oct 09 '12 at 18:42
  • 1
    Sorry I missed your invite, seems as though you've figured it out though? – potench Oct 09 '12 at 22:10