@@ -12,12 +12,21 @@ angular.module('openfb', [])
1212 . factory ( 'OpenFB' , function ( $rootScope , $q , $window , $http ) {
1313
1414 var FB_LOGIN_URL = 'https://www.facebook.com/dialog/oauth' ,
15+ FB_LOGOUT_URL = 'https://www.facebook.com/logout.php' ,
16+
1517
1618 // By default we store fbtoken in sessionStorage. This can be overriden in init()
1719 tokenStore = window . sessionStorage ,
1820
1921 fbAppId ,
20- oauthRedirectURL ,
22+
23+ context = window . location . pathname . substring ( 0 , window . location . pathname . indexOf ( "/" , 2 ) ) ,
24+
25+ baseURL = location . protocol + '//' + location . hostname + ( location . port ? ':' + location . port : '' ) + context ,
26+
27+ oauthRedirectURL = baseURL + '/oauthcallback.html' ,
28+
29+ logoutRedirectURL = baseURL + '/logoutcallback.html' ,
2130
2231 // Because the OAuth login spans multiple processes, we need to keep the success/error handlers as variables
2332 // inside the module instead of keeping them local within the login function.
@@ -29,6 +38,9 @@ angular.module('openfb', [])
2938 // Used in the exit event handler to identify if the login has already been processed elsewhere (in the oauthCallback function)
3039 loginProcessed ;
3140
41+ console . log ( oauthRedirectURL ) ;
42+ console . log ( logoutRedirectURL ) ;
43+
3244 document . addEventListener ( "deviceready" , function ( ) {
3345 runningInCordova = true ;
3446 } , false ) ;
@@ -66,25 +78,14 @@ angular.module('openfb', [])
6678
6779 loginProcessed = false ;
6880
69- logout ( ) ;
70-
71- // Check if an explicit oauthRedirectURL has been provided in init(). If not, infer the appropriate value
72- if ( ! oauthRedirectURL ) {
73- if ( runningInCordova ) {
74- oauthRedirectURL = 'https://www.facebook.com/connect/login_success.html' ;
75- } else {
76- // Trying to calculate oauthRedirectURL based on the current URL.
77- var index = document . location . href . indexOf ( 'index.html' ) ;
78- if ( index > 0 ) {
79- oauthRedirectURL = document . location . href . substring ( 0 , index ) + 'oauthcallback.html' ;
80- } else {
81- return alert ( "Can't reliably infer the OAuth redirect URI. Please specify it explicitly in openFB.init()" ) ;
82- }
83- }
84- }
81+ // logout();
82+
83+ if ( runningInCordova ) {
84+ oauthRedirectURL = 'https://www.facebook.com/connect/login_success.html' ;
85+ }
8586
8687 loginWindow = window . open ( FB_LOGIN_URL + '?client_id=' + fbAppId + '&redirect_uri=' + oauthRedirectURL +
87- '&response_type=token&display=popup&scope=' + fbScope , '_blank' , 'location=no' ) ;
88+ '&response_type=token&display=popup&scope=' + fbScope , '_blank' , 'location=no,clearcache=yes ' ) ;
8889
8990 // 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
9091 if ( runningInCordova ) {
@@ -138,7 +139,20 @@ angular.module('openfb', [])
138139 * Application-level logout: we simply discard the token.
139140 */
140141 function logout ( ) {
141- tokenStore [ 'fbtoken' ] = undefined ;
142+ var logoutWindow ,
143+ token = tokenStore [ 'fbtoken' ] ;
144+
145+ /* Remove token. Will fail silently if does not exist */
146+ tokenStore . removeItem ( 'fbtoken' ) ;
147+
148+ if ( token ) {
149+ logoutWindow = window . open ( FB_LOGOUT_URL + '?access_token=' + token + '&next=' + logoutRedirectURL , '_blank' , 'location=no,clearcache=yes' ) ;
150+ if ( runningInCordova ) {
151+ setTimeout ( function ( ) {
152+ logoutWindow . close ( ) ;
153+ } , 700 ) ;
154+ }
155+ }
142156 }
143157
144158 /**
@@ -168,7 +182,7 @@ angular.module('openfb', [])
168182
169183 params [ 'access_token' ] = tokenStore [ 'fbtoken' ] ;
170184
171- return $http ( { method : method , url : 'https://graph.facebook.com' + obj . path , params : params } )
185+ return $http ( { method : method , url : 'https://graph.facebook.com' + obj . path + '?' + toQueryString ( params ) , params : params } )
172186 . error ( function ( data , status , headers , config ) {
173187 if ( data . error && data . error . type === 'OAuthException' ) {
174188 $rootScope . $emit ( 'OAuthException' ) ;
@@ -207,6 +221,16 @@ angular.module('openfb', [])
207221 return obj ;
208222 }
209223
224+ function toQueryString ( obj ) {
225+ var parts = [ ] ;
226+ for ( var i in obj ) {
227+ if ( obj . hasOwnProperty ( i ) ) {
228+ parts . push ( encodeURIComponent ( i ) + "=" + encodeURIComponent ( obj [ i ] ) ) ;
229+ }
230+ }
231+ return parts . join ( "&" ) ;
232+ }
233+
210234 return {
211235 init : init ,
212236 login : login ,
0 commit comments