Facebook FB.login not awaiting response - facebook

given the following code: ` const handleFacebookLogin = (e: any) => {
console.log('called login')
FB.getLoginStatus(function (response) {
console.log('login status response', response)
if (response.status === 'connected') {
// Logged into your app and Facebook.
console.log('logged in correctly')
} else {
window['FB'].login(function (response2) {
console.log('logged in', response2)
}, fbOptions)
}
})
console.log('finished click handler')
}`
the response returning response2 happens before the user dialog is has even been filled out. I would think that FB would wait for a user name and password BEFORE calling the callback function, or am I crazy?
I have tried everything I can find on SO and all other resources I can find. I would expect that once a user enters FB credentials and the call is made THEN the callback function would happen instead of a linear execution of code.

Related

Getting all Pages user is Admin of using Graph API

I am trying to get a list of all the pages the logged user an Admin of, but I am getting the error "An active access token must be used to query information about the current user". Here is the code I am using:
function login(){
FB.login(function(response) {
if (response.authResponse) {
$.get("https://graph.facebook.com/me/accounts", function(data) {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function(data) {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
} else {
// not auth / cancelled the login!
}
}, { scope: "manage_pages" });
}
So in this code, after logging in, the call $.get("https://graph.facebook.com/me/accounts") throws the error.
UPDATE:
I am able to get the the list using FB.api("/me/accounts"). So how come I can't get it using this code? What am I doing wrong? Thanks.
Simple: You are not passing a user access token with your direct request:
$.get("https://graph.facebook.com/me/accounts", function(data) { ... });
instead of
var accessToken = "aoidhfgoafhgoidfhg"; // replace with real access token
$.get("https://graph.facebook.com/me/accounts?access_token=" + accessToken, function(data) { ... });
The FB SDK does this internally/automatically. What's not really clear to me is why you don't use the FB SDK here as well, if you're already using it for FB Login. That doesn't make much sense IMHO.

what is response in FB.getLoginStatus

To detect the facebook user online/offline status we use the method FB.getLoginStatus method.
But, what does the paramater("response") mean and where does it come from in the below code snippet
the parameter response mean in the line " FB.getLoginStatus(function(response) "
FB.getLoginStatus(function(response) {
console.log(response);
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
console.log('User logged in and autenticated');
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
console.log('User logged in, but not autorized');
} else {
// the user isn't logged in to Facebook.
console.log('User not logged in');
}
}, true);
You are specifying a function to be called when the Facebook API has completed retrieving a response from the server. It passes the response object to the function which you specify. A typical response would be:
{
status: 'connected',
authResponse: {
accessToken: '...',
expiresIn:'...',
signedRequest:'...',
userID:'...'
}
}
See facebook javascript docs for more info

facebook open graph have to reload page to post to timeline

Very confused, but my open graph works. The only problem is I have to hit refresh a few times for it to actually post to timeline.
What could be the problem here?
You should add the javascript timeout function to call facebook a few seconds after the page has loaded. E.g.
setTimeout( function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// publish action to facebook
FB.api( '/me/news.reads', 'post', { article : 'http://www.yourdomain.com/your_article/' } );
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
}, 2000 );
This will allow enough time for Facebook SDK to load and initialise, before posting your action off to facebook.

Facebook - FB.getLoginStatus

I am trying to use Facebook javascript library function : FB.getLoginStatus.
This function works when the user is logged but fails when user is not logged to facebook.
Here is my code:
FB.getLoginStatus(function (response) {
console.log('entering the function!');
if (response.status === 'connected') {
FB.api('/me', function (response) {
if (response.name != undefined) {
console.log("Welcome, " + response.name);
}
});
} else if (response.status === 'not_authorized') {
console.log('not logged to app');
} else {
// NEVER POPUP
console.log('user not logged');
}
});
The last event (user not logged never popup), when a user is not logged, the function is not event firing and I do not see the log [entering the function].
Do someone has an idea to detect if a facebook user is connected.
Regards,
Linvi
I sent a bug report for this issue and Facebook developer reported that it was actually a current known issue.

fired event when user allow the app

This event is fired when user login
FB.Event.subscribe('auth.login', function(response) {
//
});
But i am just looking for the event that is fired when user allows the app from auth dialog ?
FB.Event.subscribe('auth.authResponseChange', function(response) {
alert('The status of the session is: ' + response.status);
});
auth.authResponseChange - fired when the authResponse changes, so in theory when a user allows the app from the auth dialog this will fire.
from http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/
Also, in the code to prompt the user to allow the app:
FB.getLoginStatus(function(response) {
if (response.authResponse) {
// logged in and connected user, someone you know
} else {
// no user session available, someone you dont know
FB.login(function(response) {
if (response.authResponse) {
console.log(reponse);
// here will be all the response details from confirmed app
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
}
});