Skip to content

Instantly share code, notes, and snippets.

@jazeabby
Created June 23, 2019 07:05
Show Gist options
  • Save jazeabby/59a47f5882b2184327e6cd7f4b4d3ff3 to your computer and use it in GitHub Desktop.
Save jazeabby/59a47f5882b2184327e6cd7f4b4d3ff3 to your computer and use it in GitHub Desktop.
#add these primary resources to include Facebook
<div id="fb-root"></div>
# button for facebook
<button class="" onclick="_login();" ><i class="fa fa-facebook-official fa-2x" aria-hidden="true"></i>
Continue with Facebook
</button>
# javascript/jquery
<script type="text/javascript">
// Load the SDK asynchronously
(function(thisdocument, scriptelement, id) {
var js, fjs = thisdocument.getElementsByTagName(scriptelement)[0];
if (thisdocument.getElementById(id)) return;
js = thisdocument.createElement(scriptelement); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
window.fbAsyncInit = function() {
FB.init({
appId : '{{your-fb-api-key}}',
cookie : true,
xfbml : true,
version : 'v3.3'
});
};
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
if (response.status === 'connected') {
// Logged into your app and Facebook.
_i();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
}
}
// called on click of button
function _login() {
FB.login(function(response) {
// handle the response
if(response.status==='connected') {
// inbuilt function to login to my application as facebook is conntected to out application
_i();
}
}, {scope: 'public_profile,email'});
}
function fb_logout(){
FB.logout(function(response) {
alert('logged out successfully');
});
}
//facebook login
function _i(){
FB.api('/me?fields=email,name,picture', function(response) {
// for more reference of fields
// https://developers.facebook.com/docs/facebook-login/permissions#reference-default
$.post("localhost/login/validate",
{
facebook_login: 'true',
email: response.email,
name: response.name,
fb_id:response.id,
photo:response.picture
},
function(data){
data = JSON.parse(data);
// redirect as per success in data
});
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment