Facebook Application - Unable to get inside FB.getLoginStatus and execute something - facebook

I'm using the following code:
alert('Inside login status');
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
//user is logged in and connected
userid = response.authResponse.userID;
accesstoken = response.authResponse.accessToken;
alert('Welcome' + response.authResponse.name);
//return 'connected'
//do something with user
}
else if (response.status === 'not_authorized') {
//user is logged into facebook but not connected to the app
//do something
alert('Like us to get started');
//return 'not_authorized'
}
else {
//the user even connected to facebook
//button.innerHTML = 'unknown'
//button.onclick = function () { login(); }
alert('login to get started');
//login();
//return 'unknown'
}
});
I have defined the above code in a function that is called in the async init. (The async init is defined in the code behind, I'm registering the script to the head of the page on page load) The first alert is called which means I get into the function, but the rest of it doesn't seem to work. No alerts are displayed after that.
On debugging using breakpoints, I was just directed to the end of the function. No clue why this happens. Am I missing something?

Make sure that you have specified correct callback URL in facebook application setting. And If you are testing it on local machine, have you created virtual directory?

It's just the way it runs on my system. I deployed it to a different environment and it worked like a charm.

Related

Facebook login popup blocked

I am using a Facebook app to authenticate my users, but if the user is not logged in to Facebook (I am checking it with FB.getLoginStatus()) I show him a button to log in with. The problem is that the pop-up gets blocked all the time. I have no idea why, since I am registering the Facebook log in action on ng-click.
<button type="button" ng-click="login()">Log in via Facebook</button>
...
$scope.login = function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
$scope.userId = response.authResponse.userID
loginSuccess()
} else
FB.login(function(response) {
if (response.authResponse) {
$scope.userId = response.authResponse.userID
loginSuccess()
} else {
alert('You need to log in and authorize the app, otherwise you won\'t be able to take the quiz!')
}
})
})
}
Any help?
FB.getLoginStatus should be used on page load to check if the user is authorized and to refresh the User Token. You can use it right after FB.init and store the User ID if he is logged in.
FB.login must be used directly on user interaction, you are using it in the asynchronous (!) callback function of FB.getLoginStatus and not directly when the user clicks on the button.
Example: http://www.devils-heaven.com/facebook-javascript-sdk-login/

What to do after getting 'not_authorized' back from Facebook Connect login

After your get your response from getLoginStatus, is the idea to then call FB.login? Currently when I do that, the code stops.
Here's the code for when they hit the FB button to login, which correctly gives me the response...
function facebookConnectBtnLogin()
{
FB.getLoginStatus(function(response)
{
CL();
if (response.status === 'connected') {
logFacebookUserIn(response);
} else if (response.status === 'not_authorized') {
// alert('not_authorized...');
facebookLoginInit();
} else {
//alert('not logged into Facebook...');
facebookLoginInit();
}
},true);
}
And the code that deals with FB.login, which alerts 'here4', but not 'here5'...
function facebookLoginInit()
{
alert('here4');
FB.login
(
function(response)
{
alert('here5');
CL();
if(response.authResponse)
{
alert('here6'); logFacebookUserIn(response);
} else alert('not connected');
},
{ scope: "email" }
);
}
Do I have the right idea?
Thanks for your time and help.
You should never call FB.login in an asynchronous callback function, ONLY on direct user interaction (mouse click). Browsers block the FB.login popup if you don´t call it on user interaction.
With FB.getLoginStatus you just check if the user is authorized, if not: present a Login Button where the user can click to login. It´s not a good idea to show the login dialog right when the user enters your App anyway, tell him what the App is about first.

Emberjs: check if user is logged with facebook on init

I'm trying to use Facebook to log in an Ember application. The login action is working, but I can't find how to check if the user has already logged in when the app is initialized.
I'm calling the auth.authResponseChange event but it blocks the app and it never starts.
App.ApplicationController = Ember.Controller.extend({
fbUser: false,
init: function() {
this.checkFBUser()
},
checkFBUser: function() {
FB.Event.subscribe('auth.authResponseChange', function(response) {
// this is never triggered and the app does not init
console.log(response)
if (response.status === 'connected') {
console.log('Logged in')
}
else {
FB.login()
}
})
},
// this works
actions: {
loginFacebook: function() {
var self = this
FB.login(function(response) {
if (response.authResponse) {
FB.api('/me', function(response) {
self.set('fbUser', response)
})
}
})
}
}
})
How am I supposed to do this?
I haven't worked with FB lately, but I'm pretty sure the AuthResponeChange event is for being notified of a change in the AuthResponse status; in this instance you'll probably want to take a look at FB.getLoginStatus instead.
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus
checkFBUser: function()
{
FB.getLoginStatus(function(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;
} 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.
}
});
I've found the source of the problem and a solution. I'm not sure this is the best solution so I'm open to other suggestions but if this can help other people, here is the thing:
The issue was that the Facebook library was not loaded yet, so the FB variable was undefined. For some reason if you try to do something with an undefined variable in Ember the app just won't work without even a warning. So the solution is to wait:
App.deferReadiness()
function checkFB() {
if (typeof FB !== 'undefined') {
APp.advanceReadiness()
}
else {
setTimeout(checkFB, 10);
}
}
setTimeout(checkFB, 10);
10 milliseconds is pretty short so the app is still initialized very quick, and in my tests the app starts after ~20 milliseconds. This still feels a bit hacky but in place of a better solution this will do for now.
As Aaron said you need to use fb.getLoginStatus to check if a user is already logged in. To make sure Facebook is loaded before you to make any calls you can use an initializer and defer the app until the SDK has loaded.
http://nerdyworm.com/blog/2013/04/03/ember-initializers/
Ember.Application.initializer({
name: 'init_fb',
initialize: function (container) {
window.App.deferReadiness();
Ember.$.getScript( '//connect.facebook.net/en_UK/all.js', function( ) {
FB.init( {
appId : 'xxxxxxxxxxxxx',
xfbml : true,
version : 'v2.0',
} );
window.App.advanceReadiness();
} );
}
});

FB.getLoginStatus returns connected in every situation

I am building an iphone app with phonegap, and using the facebook javascript sdk.
FB.getLoginStatus(function(response){
if(response.status === 'connected'){
alert("connected");
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
}
else if(response.status === 'not_authorized'){
login();
}
else{
login();
}
});
This function checks the status. But it alerts "connected" even i am not logged in to facebook with the xcode iphone simulator.
I think this happens because you are logged on, maybe on browser or FB app. In my case, when I tested on smartphone, FB.getLoginStatus method always alerted 'connected' because I was logged on FB app.

FB undefined while using getLoginStatus in page show

I am getting FB undefined , when i write following code , i have successfully added the necessary files for facebook using window.fbAsyncInit , the thing is if i write following code under "click" event then it works well but not at the time of pageshow in jquery mobile. this is happening might be because the facebook js files are loading bit late. but i want to check the user status on every page to restrict unnecessary or anonymous access.
$("#details").live( "pageshow", function() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and connected to 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("INSIDE ME");
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
//but not connected to the app
console.log("OUTSIDE ME");
} else {
// the user isn't even logged in to Facebook.
}
});
});
Put the jQuery live() assignment inside the window.fbAsyncInit as well. So that way it is only applied to the DOM element after FB has a chance to initialize.