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