Facebook - FB.getLoginStatus - facebook

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.

Related

Facebook FB.login not awaiting response

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.

Facebook getLoginStatus gets status: "unknown" even when logged into Facebook

I am trying to ascertain whether a user is logged into Facebook using the JS SDK.
The call to FB.getLoginStatus always returns status="unknown", even when I know
I am logged into Facebook - literally, I have my Facebook home page open in the
next browser tab.
I am loading the API like this:
<script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js"></script>
And in my JS code:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
function statusChangeCallback( response )
{
console.log("login status change callback: ", response);
if (response.status === 'connected')
{
testAPI();
}
else if (response.status === 'not_authorized')
{
// Not authorized, so show the login button
show_block( ".loginScreen" );
}
else
{
// nothing doing
show_block( ".loginScreen" );
}
} // end statusChangeCallback()
window.fbAsyncInit = function() {
FB.init({
appId : <my app id>,
cookie : true,
xfbml : true,
status : true,
version : 'v12.0'
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
My console window shows:
login status change callback: {authResponse: null, status: 'unknown'}
Why would I be getting a "unknown" status here? I have Facebook open in the next browser tab, so I know that I'm logged in.

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.

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'});
}
});