FB Login : Don't know how to get response after a successful login - facebook

I have added an fblogin button to my website
it is working fine
a button is placed and when user clicks on it a dialogue box opens for login.
But i don't know how to get response after a successful login.
Please suggest

You can use the JS SDK to subscribe to events of that type. Take a look at the facebook javascript documentation, the method you are looking for is 'auth.authResponseChange' :
FB.Event.subscribe('auth.authResponseChange', function(response) {
alert('The status of the session is: ' + response.status);
});

Related

Chrome blocks FB.login()

I have a FB app which need to be integrated in a FB page tab. In app I want to provide a "Signup with FB login" option. This button when clicked should prompt the user to login into FB login dialog. On successful login it should prompt the user to authenticate and allow the app to use his details. Once the user allows the app to access the user details it should then post the details to my website in a new window.
This process works fine when I test the app independently. However when I add the app to the fb page, chrome blocks the Fb login dialog. Before opening the Fb login dialog I check if the user is already logged in FB and has accepted the app. For that I use FB.getLoginStatus(checkLoginStatus); I figured due to this check the context moves to script execution and hence Chrome blocks the login dialog.
Is there a work around for this issue? Help would be highly appreciated.
My code is as follows:
The facebook button is created using span and the id fbc-login-button is given to an a tag.
$("#fbc-login-button").click(function(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
if (response.authResponse) {
fbAppSignup(response);
}
}
else {
FB.login(function(response) {
if (response.authResponse) {
fbAppSignup(response);
}
},{scope: "email, user_friends",display:"popup"});
}
});
function fbAppSignup(response,myPopup){
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api("/me", function(response) {
$("#social_media_data").val(JSON.stringify(response));
$("#medium").val("facebook");
$("#accessToken").val(accessToken);
$("#socialClickSource").val("fbapp_facebook_signup");
$("#fbSignUp").submit();
return true;
});
}
The example in the Facebook docs is a bit misleading, you should never use FB.login in an asynchronous callback function - and that is exactly what you are doing right now, because FB.getLoginStatus is asynchronous. Always call FB.login on user interaction (mouse click) or it will get blocked by intelligent browsers.
FB.getLoginStatus: on page load, for refreshing the user session and
checking if the user is authorized already
FB.login: on user interaction
Other threads i´ve answered about that problem:
Facebook login popup blocked
FB.api response callback async popup blocked
Sign In with FB issue: FB popup window with the Login dialog is blocked by a web browser

how can I prevent native facebook login dialog in ios?

I am using facebooksdk.framework 3.1
when I login my app with my account, native login dialog pops up even though web based auth
completed. I need to turn off either native login dialog or web based auth but I don't know how.
I tried to find facebook.m file but there was no such file on facebooksdk.framework 3.1
how do I turn off safariauth or native auth?? It is very strange two login process occurs at the
same time.
First of all go to facebook.m page & try to find the following method:
- (void)authorize:(NSArray *)permissions
delegate:(id<FBSessionDelegate>)delegate
localAppId:(NSString *)localAppId
in this method there is a line:[self authorizeWithFBAppAuth:YES safariAuth:NO];
change authorizeWithFBAppAuth & safariAuth to yes/no according to your need.Hope it helps you.
Or you can try to implement latest Share kit sdk.it is best way.
If you use phonegap facebook plugin, you should call FB.getLoginStatus() carefully.
If FB.getLoginStatus() fires before FB.init() is done, the function would return a response as
'not connected' even if user is already connected.
my login problem was due to FB.getLoginStatus() on my redirected page.
even if user succed login and procceded to my redirected page, the page run FB.getLoginStatus()
and result was always 'not connected' because FB.init() not completely executed.
facebook provides async function to solve this problem but it didn't work when I tested on phonegap.
to check user login, I use FB.Event.subscribe and localstorage for now.
on the page that needs FB functions, I added this javascript code
FB.Event.subscribe('auth.login', function(response){
localStorage.setItem('fblogin', true);
console.log('login event');
}
FB.Event.subscribe('auth.logout', function(response){
localStorage.setItem('fblogin', false);
console.log('logout event');
}
document.addEventListener('deviceready', function(){
try{
FB.init({ appId : '1234567889' , nativeInterface : CDV.FB, useCachedDialogs : true});
}catch(e){
alert(e);
}
var fbval = localStorage.getItem('fblogin');
if(fbval){
// your code for connected status
}
});
basically, I set 'fblogin' localStorage value true whenever login event occurs,
and set the value 'false' whenever logout event occurs.
by comparing 'fblogin' value, I check users are logged or not.

How to detect Facebook oauth window closed action from user

I am trying to figure if there is any way to notify the parent page when user either closes or clicks on cancel button on Facebook permissions window during oauth flow. Its the window where user authorizes an app. The window with url: https://www.facebook.com/dialog/oauth?api_key=..... and with "Login with Facebook" and "Cancel" buttons. What I am trying to accomplish is-- If user cancels or closes the oauth permissions window, I would like to open Facebook simple sharing window where they can post status without authorizing the app.
I am using JS SDK for Facebook connect.
This code is taken from the documentation for the FB.login method:
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.');
}
});
As you can see, the if (response.authResponse) part checks rather the login process was finished successfully, that is, the user allowed the application and he is logged in to facebook.
If the user clicked "cancel" or closed the popup then there won't a authResponse parameter in the response.
Observe the url as user cancel the authorization the oauth url changes, it will now contain number of fields to describe user action
YOUR_REDIRECT_URI?
error_reason=user_denied
&error=access_denied
&error_description=The+user+denied+your+request.
&state=YOUR_STATE_VALUE
This will be the value after user cancel the authentication
error_reason=user_denied
&error=access_denied
&error_description=The user denied your request
After user cancel's the authentication dialog page will be redirected to the parent page where you can check these error parameters.

Facebook application: FB events work only for first time

I'm using the following code to subscribe to fb login and logout events.
FB.Event.subscribe('auth.logout', function(response) {
RefreshPageOnFBStatusChange(response);
});
FB.Event.subscribe('auth.login', function(response) {
RefreshPageOnFBStatusChange(response);
});
function RefreshPageOnFBStatusChange(response)
{
alert(response.status);
}
I want to redirect users to different pages when they are logged in or logged out. The function I'm using here gets called only during the actual page load, and never after that. I have a fb-like, fb-share and live stream plugin on a page. When I login and logout of live stream, nothing happens. Help!
Please modify your question and provide all FB-related code.
This may answer you question:
Facebook FB.Event.subscribe event does not fire:(
For User Status you can use if(FB._userStatus == "connected"){} to check user is login or not And for Logout & login you can use following simple code
FB.logout(function(response) { window.location = redriect_uri;
});
It seems like these events don't fire when you use the live chat plugin because it has its own way of handling the login and logout. What I had to do to solve this problem was to set a timeout of 5 seconds and call a javascript function that did a FB.GetLoginStatus, and based on what it was I would refresh the page. Not the best way of doing it, but I was left with no choice.

Detecting if a facebook user allowed or disallowed login

I am using FB's Javascript SDK using the login-button:
<fb:login-button onlogin="onFBLogin();">
How can I detect within onFBLogin() if the user did log in successfully ? Currently it is called everytime, no matter what the user chooses.
Thanks,
Meir
I think the first parameter passed into onFBLogin() is going to be either a response or a session object. So check to see what the value of response or response.session is.
Also, have you played around with FBJS's login event handling?
If using FB.login:
FB.login(function(response) {
if (response.session) {
// user successfully logged in
} else {
// user cancelled login
}
});
But since you're using an fb:login button, you can try to subscribe to the login event:
FB.Event.subscribe('auth.login', function(response) {
// do something with response.session
});
I haven't tried this myself via fb:login button, but hopefully this will get you on the right track.