Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
<title>Facebook Login JavaScript Example</title>
<meta charset="UTF-8">
window.fbAsyncInit = function() {
appId : '572516629527309',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.1' // use version 2.1
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); = id;
js.src = "//";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function fb_login(){
FB.login(function(response) {
if (response.authResponse) {
console.log('Welcome! Fetching your information.... ');
//console.log(response); // dump complete info
access_token = response.authResponse.accessToken; //get access token
user_id = response.authResponse.userID; //get FB UID
FB.api('/me?fields=id,name,friendlists', function(response) {
user_email =; //get user email
// you can store this data into your database
} else {
//user hit cancel button
console.log('User cancelled login or did not fully authorize.');
}, {
scope: 'publish_stream,email,read_friendlists'
<a href="#" onclick="return fb_login();">
<img src="">
<div id="status"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment