No need for a wrapper. Utilize $firebaseAuth()
along with $http()
for seamless Graph API requests.
The Graph API is user-friendly and integrates smoothly with Firebase.
Ensure that you have granted the Facebook Friends permission; otherwise, you will not receive any data in return.
You can employ $firebaseAuth()
to log in and acquire the Facebook access_token
. This token can then be utilized with the Graph API for retrieving data via HTTP requests, which Angular simplifies through its $http
library.
My coding style may differ, as I prefer following the guidelines of the Angular styleguide.
angular.module('app', ['firebase'])
.constant('FirebaseUrl', 'https://<my-firebase-app>.firebaseio.com/')
.constant('FacebookAppId', '<app-id>')
.service('RootRef', ['FirebaseUrl', Firebase])
.factory('Auth', Auth)
.factory('Friends', Friends)
.controller('MainCtrl', MainCtrl);
function Friends($http, RootRef, $q, FacebookAppId) {
function getFriends() {
// Retrieve the currently logged-in user (could be null)
var user = RootRef.getAuth();
var deferred = $q.defer();
var token = null;
var endpoint = "https://graph.facebook.com/me/friends?access_token="
// If no user is logged in, reject with an error and return the promise
if (!user) {
deferred.reject('error');
return deferred.promise;
} else {
// User is present, retrieve the token
token = user.facebook.accessToken;
// Append the token to the endpoint
endpoint = endpoint + token;
}
// Initiate the HTTP call
$http.get(endpoint)
.then(function(data) {
deferred.resolve(data);
})
.catch(function(error) {
deferred.reject(error);
});
// Return the promise
return deferred.promise;
}
return {
get: getFriends
};
}
Friends.$inject = ['$http', 'RootRef', '$q', 'FacebookAppId'];
function Auth($firebaseAuth, RootRef) {
return $firebaseAuth(RootRef);
}
Auth.$inject = ['FirebaseAuth', 'RootRef'];
function MainCtrl($scope, Friends) {
$scope.login = function login() {
Auth.$authWithOAuthPopup('facebook').then(function(authData) {
console.log(authData, 'logged in!');
});
};
$scope.getFriends = function getFriends() {
Friends.get()
.then(function(result) {
console.log(result.data);
});
};
}
MainCtrl.$inject = ['$scope', 'Friends'];