Skip to content

Instantly share code, notes, and snippets.

@simonmorley
Created January 23, 2015 00:51
Show Gist options
  • Save simonmorley/347e8378ab68f668a211 to your computer and use it in GitHub Desktop.
Save simonmorley/347e8378ab68f668a211 to your computer and use it in GitHub Desktop.
Medium, creating a facebook login, facebook directive v. basic
'use strict';
var app = angular.module('ctLoginsApp.facebook.directives', ['ngResource']);
app.directive('facebook', ['$window', '$compile', '$q', function($window, $compile, $q) {
var link = function(scope,element,attrs) {
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '{your-app-id}',
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
});
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
// Load the SDK asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
};
return {
link: link,
scope: {
appId: '@'
},
template: '<div>Loading...</div>'
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment