Skip to content

Instantly share code, notes, and snippets.

@seinoxygen
Created October 20, 2014 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seinoxygen/4c5cc985a8e9353af0b0 to your computer and use it in GitHub Desktop.
Save seinoxygen/4c5cc985a8e9353af0b0 to your computer and use it in GitHub Desktop.
Suscribe via Facebook Connect
<div id="fb-root"></div>
<script src="http://connect.facebook.net/es_ES/all.js"></script>
<script>
$(document).ready(function(){
FB.init({
appId : 'APP_ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
$('#fb-login').click(function(e){
e.preventDefault();
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
getCurrentUserInfo(response)
} else {
FB.login(function(response) {
if (response.authResponse){
getCurrentUserInfo(response)
} else {
console.log('Auth cancelled.')
}
}, { scope: 'email' });
}
});
});
$('#fb-logout').click(function(e){
e.preventDefault();
FB.getLoginStatus(function(response) {
if (response && response.status == 'connected') {
FB.logout(function(response) {
document.location.reload();
});
}
});
});
function getCurrentUserInfo() {
FB.api('/me', function(userInfo) {
console.log(userInfo.name + ': ' + userInfo.email);
$.ajax({
url: '/newsletter.php',
type: 'post',
data: {nombre: userInfo.name, email: userInfo.email},
dataType: 'json',
success: function(data){
if(data.error == 0){
alert("Gracias por suscribirse.");
}
}
});
});
}
});
</script>
<button id="fb-login">Login to Facebook</button>
<button id="fb-logout">Logout from Facebook</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment