Created
October 20, 2014 16:22
-
-
Save seinoxygen/4c5cc985a8e9353af0b0 to your computer and use it in GitHub Desktop.
Suscribe via Facebook Connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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