Facebook Login Custom Designed Button - facebook

Is it possible to use the the facebook login plugin with a custom UI button on my side? I dont want to use the standard FB plugin that comes with the plugin.
I want to be able to click a custom button and have it do the same action, launch pop/modal to sync.
Any help would be great.

Sure you can! But you'll have to do some JavaScript coding for it. If you use the Facebook JavaScript SDK you'll be able to log your users into your website using any element that you can trigger a JavaScript function with.
Check out the FB.login() function :
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 + '.');
FB.logout(function(response) {
console.log('Logged out.');
});
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'email'});
This function will initiate a login popup for users that have not yet logged in and request their email addresses.
It's more work than just pasting the plugin, but if you want to customize the login features then this is the way you will accomplish it.

Related

Facebook Authorization redirecting to "The page you requested was not found" on mobile devices

I've been building out a social gifting platform, and have had an application running for a few months now at: http://egift.me/promos/venue/facebookconnect
There is a simple user flow for the app. Click the Login/Connect facebook button, login &/or authorize the application, and you're redirected to a 'thank you/confirmation' style page.
If I step through this on a standard browser, I have no problems. If I step through this on mobile, I ultimately get the lovely "The page you requested was not found" page.
I don't have a Mobile Web url configured, I'm simply using Website with Facebook Login. Even when I have tried adding a mobile web url (it's the same base URL, I'm using View Switching to serve up a desktop vs mobile optimized view), I get the same issue.
Anyone have any idea what's going on? Is there any additional information I can provide?
[UPDATE]
This works (be sure to change your scope):
//instead of onClick could just as easily use something like jQuery event binding
<button onClick="loginUser();">Login</button>
<script type="text/javascript">
function loginUser() {
FB.login(function (response) { }, { scope: 'YOUR_SCOPE_HERE' });
}
FB.getLoginStatus(function (response) {
FB.Event.subscribe('auth.authResponseChange', handleResponse);
});
handleResponse = function (response) {
document.body.className = response.authResponse ? 'connected' : 'not_connected';
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
} else if (response.status === 'not_authorized') {
// the user is logged in to FB, but has not yet authenticated your app
} else {
// the user is not logged in to FB
}
};
</script>
For mobile web apps, I'd recommend to use FB.login via the SDK itself rather than through the use of the login button. If nothing else it gives you greater programmatic control of the user flow & experience.
The canonical "simplest ever social app" snippet is:
FB.login(function(response) {
if (response.authResponse) {
console.log('Fetching user info');
FB.api('/me', function(response) {
console.log('Hello ' + response.name + '!');
});
} else {
console.log('User cancelled login or did not fully authorize');
}
});
Also you're including the JS SDK for the plugin anyway, so there's no payload overhead. More details on FB.login here: https://developers.facebook.com/docs/reference/javascript/FB.login/

Explain many inner-most loops in FB.login to get the access token?

My friend called this like lambda-calculus, even though I know some lambas -- I am still confused how the authorization really works to get the access token.
So please explain this code, hopefully line-by-line. Source of the example is here.
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.');
}
});
P.s. Trying to categorize things here, the q is a sub-problem.
Perhaps related
Explain the Facebook access_token
Facebook get access Token
FB.login(function(response) {
FB is where all of the Facebooks functions live. All the methods are defined here: https://developers.facebook.com/docs/reference/javascript/ . Login in particular will popup a dialog asking the user to sign in or approve your app.
if (response.authResponse) {
If they approve your app or have it already added, the response.authResponse will be populated. authResponse also has accessToken, expiry, and UserID. See more details here: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
This goes to Facebook to ask for the user who just approved/signed into your app. Now you can get the basic information about the user. All the available fields with permissions you need to get them are available here: https://developers.facebook.com/docs/reference/api/user/
console.log('Good to see you, ' + response.name + '.');
This is just getting the name from the user object that you recieved from the /me endpoint.
});
} else {
console.log('User cancelled login or did not fully authorize.');
This means the user cancelled or did not approve your app so you cannot grab any of their information from Facebook.
}
});

is Facebook js sdk Sufficient to authenticate users in my site?

in my site i Login using facebook js sdk, ( I tried the example in facebook page), but how can i tell the server that this user is authenticated ?.
I tried using ajax to post to the server .
but this seems unsecure !.
My question is the authentication process can be donr using only JS sdk ?
If the authentication process cannot be done using Js ,
what about facebook c# sdk or other unofficial facebook c# sdk.
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 + '.');
// POST TO SERVER AND TELL HIM THAT THE USER IS AUTHENTICATED NOW
});
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
The JS SDK can set a cookie on login, that you can read out server-side. If you do another call to /me there, then you can be relatively sure everything’s OK.

auto detect facebook session in FB connect sites

a simple problem is that i want when user logged in to Facebook so user automatically
logged in to my sites.i have FBconnect implemented in my site .no refresh the page..
FB.getLoginStatus(function(response) {
if (response.session) {
//what to do here???
//i dont want to use setTimeout() in this function to check it again & again
}
});
any idea how to do this???
It seems like you need...
FB.Event.subscribe('auth.login', function(response) {
// do something with response
});
https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

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.