Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fixes and improvements #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ A sample application that demonstrates a lightweight approach to integrate with

3. Replace the www folder of the Ionic project with the www folder in this repository

4. Create a Facebook app here: https://developers.facebook.com/apps. In the advanced settings, make sure you declare a “Valid OAuth redirect URI”. For example, if during development you access your application from http://localhost/openfb/index.html, you must declare http://localhost/openfb/oauthcallback.html as a valid redirect URI. Also add https://www.facebook.com/connect/login_success.html as a Valid OAuth redirect URI for access from Cordova.
4. Create a Facebook app here: https://developers.facebook.com/apps.
5. Config the Facebook app. If you're using default Ionic setup, follow the following steps; if not, change `http://localhost:8100/` to your local path to your `index.html`.
* Go to "Basic". Set "Site URL" to `http://localhost:8100/`
* Go to "Advanced". Add these two urls to "Valid OAuth redirect URIs":
- `https://www.facebook.com/connect/login_success.html` (for access from Cordova)
- `http://localhost:8100/oauthcallback.html` (for access from local machine)

5. Copy the Facebook App Id and paste it as the first argument of the OpenFB.init() method invocation in the run() function in app.js.

Expand Down
4 changes: 2 additions & 2 deletions www/js/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ angular.module('sociogram.controllers', [])

$scope.facebookLogin = function () {

OpenFB.login('email,read_stream,publish_stream').then(
OpenFB.login('email,read_stream,publish_actions,user_friends').then(
function () {
$location.path('/app/person/me/feed');
},
Expand Down Expand Up @@ -63,7 +63,7 @@ angular.module('sociogram.controllers', [])
})

.controller('FriendsCtrl', function ($scope, $stateParams, OpenFB) {
OpenFB.get('/' + $stateParams.personId + '/friends', {limit: 50})
OpenFB.get("/me/friends", {limit: 50})
.success(function (result) {
$scope.friends = result.data;
})
Expand Down
64 changes: 44 additions & 20 deletions www/js/openfb-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ angular.module('openfb', [])
.factory('OpenFB', function ($rootScope, $q, $window, $http) {

var FB_LOGIN_URL = 'https://www.facebook.com/dialog/oauth',
FB_LOGOUT_URL = 'https://www.facebook.com/logout.php',


// By default we store fbtoken in sessionStorage. This can be overriden in init()
tokenStore = window.sessionStorage,

fbAppId,
oauthRedirectURL,

context = window.location.pathname.substring(0, window.location.pathname.indexOf("/",2)),

baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '') + context,

oauthRedirectURL = baseURL + '/oauthcallback.html',

logoutRedirectURL = baseURL + '/logoutcallback.html',

// Because the OAuth login spans multiple processes, we need to keep the success/error handlers as variables
// inside the module instead of keeping them local within the login function.
Expand All @@ -29,6 +38,9 @@ angular.module('openfb', [])
// Used in the exit event handler to identify if the login has already been processed elsewhere (in the oauthCallback function)
loginProcessed;

console.log(oauthRedirectURL);
console.log(logoutRedirectURL);

document.addEventListener("deviceready", function () {
runningInCordova = true;
}, false);
Expand Down Expand Up @@ -66,25 +78,14 @@ angular.module('openfb', [])

loginProcessed = false;

logout();

// Check if an explicit oauthRedirectURL has been provided in init(). If not, infer the appropriate value
if (!oauthRedirectURL) {
if (runningInCordova) {
oauthRedirectURL = 'https://www.facebook.com/connect/login_success.html';
} else {
// Trying to calculate oauthRedirectURL based on the current URL.
var index = document.location.href.indexOf('index.html');
if (index > 0) {
oauthRedirectURL = document.location.href.substring(0, index) + 'oauthcallback.html';
} else {
return alert("Can't reliably infer the OAuth redirect URI. Please specify it explicitly in openFB.init()");
}
}
}
// logout();

if (runningInCordova) {
oauthRedirectURL = 'https://www.facebook.com/connect/login_success.html';
}

loginWindow = window.open(FB_LOGIN_URL + '?client_id=' + fbAppId + '&redirect_uri=' + oauthRedirectURL +
'&response_type=token&display=popup&scope=' + fbScope, '_blank', 'location=no');
'&response_type=token&display=popup&scope=' + fbScope, '_blank', 'location=no,clearcache=yes');

// If the app is running in Cordova, listen to URL changes in the InAppBrowser until we get a URL with an access_token or an error
if (runningInCordova) {
Expand Down Expand Up @@ -138,7 +139,20 @@ angular.module('openfb', [])
* Application-level logout: we simply discard the token.
*/
function logout() {
tokenStore['fbtoken'] = undefined;
var logoutWindow,
token = tokenStore['fbtoken'];

/* Remove token. Will fail silently if does not exist */
tokenStore.removeItem('fbtoken');

if (token) {
logoutWindow = window.open(FB_LOGOUT_URL + '?access_token=' + token + '&next=' + logoutRedirectURL, '_blank', 'location=no,clearcache=yes');
if (runningInCordova) {
setTimeout(function() {
logoutWindow.close();
}, 700);
}
}
}

/**
Expand Down Expand Up @@ -168,7 +182,7 @@ angular.module('openfb', [])

params['access_token'] = tokenStore['fbtoken'];

return $http({method: method, url: 'https://graph.facebook.com' + obj.path, params: params})
return $http({method: method, url: 'https://graph.facebook.com' + obj.path + '?' + toQueryString(params), params: params})
.error(function(data, status, headers, config) {
if (data.error && data.error.type === 'OAuthException') {
$rootScope.$emit('OAuthException');
Expand Down Expand Up @@ -207,6 +221,16 @@ angular.module('openfb', [])
return obj;
}

function toQueryString(obj) {
var parts = [];
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
parts.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
}
}
return parts.join("&");
}

return {
init: init,
login: login,
Expand Down
8 changes: 8 additions & 0 deletions www/logoutcallback.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<body>
<script>
// alert('closing');
window.close();
</script>
</body>
</html>