diff --git a/www/assets/js/services.account.js b/www/assets/js/services.account.js index dadc83cff..dd022a304 100644 --- a/www/assets/js/services.account.js +++ b/www/assets/js/services.account.js @@ -195,7 +195,7 @@ NGApp.factory( 'AccountHelpService', function( $http, AccountService, AccountMod service.sendForm = function(){ if( !account.isValidEmailPhone() ){ - alert( 'Please enter a valid email or phone.' ); + App.alert( 'Please enter a valid email or phone.' ); $( '.help-email' ).focus(); return; } @@ -282,9 +282,9 @@ NGApp.factory( 'AccountFacebookService', function( $http, FacebookService ){ service.facebook = FacebookService; service.account = service.facebook.account; - service.auth = function(){ + service.auth = function() { service.facebook.wait = true; - FB.login( service.facebook.startAuth, { scope: App.facebookScope } ); + FB.login(service.facebook.processStatus, { scope: App.facebookScope }); } service.signout = function( callback ){ @@ -298,18 +298,29 @@ NGApp.factory( 'AccountFacebookService', function( $http, FacebookService ){ NGApp.factory( 'AccountSignOut', function( $http, AccountFacebookService ){ var service = {}; service.facebook = AccountFacebookService; - service.do = function(){ - if (confirm( 'Confirm sign out?')){ + + // perform a logout + service.do = function() { + if (App.confirm('Confirm sign out?')) { // Force to remove the cookies - $.each( [ 'token', 'location', 'PHPSESSID' ], function( index, value ){ + $.each(['token', 'location', 'PHPSESSID'], function(index, value) { $.totalStorage(value, null); - } ); - var signout = function(){ - var url = App.service + 'logout'; - $http( { method: 'GET', url: url } ).success( function( data ) { location.href = '/'; } ); + }); + var signout = function() { + // log the session out on the server + $http.get(App.service + 'logout').success(function(data) { + console.debug('>> loged out'); + $http.get(App.service + 'config').success(function(data) { + console.debug('>> new config', data); + App.rootScope.$safeApply(function() { + App.processConfig(data); + }); + }); + + }); }; - if( service.facebook.facebook.logged || service.facebook.facebook.account.user.facebook ){ - service.facebook.signout( function(){ signout() } ); + if (service.facebook.facebook.logged || service.facebook.facebook.account.user.facebook) { + service.facebook.signout(signout); } else { signout(); } diff --git a/www/assets/js/services.facebook.js b/www/assets/js/services.facebook.js index adbb8961d..c471fc208 100644 --- a/www/assets/js/services.facebook.js +++ b/www/assets/js/services.facebook.js @@ -15,18 +15,14 @@ NGApp.factory( 'FacebookService', function( $http, $location, AccountService ){ service.account = AccountService; // This method pre load the order info that could be posted - service.preLoadOrderStatus = function( uuid ){ - var url = App.service + 'facebook/status/order/' + service._order_uuid; - $http( { - method: 'GET', - url: url, - } ).success( function( data ) { - if( data.success ){ - service.orderStatus = data.success; - } else { - service.orderStatus = false; - } - } ); + service.preLoadOrderStatus = function(uuid){ + $http.get(App.service + 'facebook/status/order/' + service._order_uuid).success( function( data ) { + if (data.success) { + service.orderStatus = data.success; + } else { + service.orderStatus = false; + } + }); } // This method let the user type his message before post @@ -57,114 +53,104 @@ NGApp.factory( 'FacebookService', function( $http, $location, AccountService ){ } ); } - service.requestLogin = function( callback ){ - FB.login( function( response ){ service.processStatus( response, callback ); }, { scope : serviceScope } ); - } - - service.registerToken = function( token ){ - if( !service.token ){ - service.token = token; - $.totalStorage( 'fbtoken', token); - } - } - - service.requestPermission = function( callback ){ + // request permission to post on a users timeline + service.requestPermission = function(callback) { + callback = typeof callback === 'function' ? callback : function(){}; FB.ui({ - method: 'permissions.request', - 'perms': serviceScope, - 'display': 'iframe' - }, - function( response ) { + method: 'permissions.request', + perms: serviceScope, + display: 'iframe' + }, callback); + } + + // process status is called any time a status change event is triggered with facebook + service.processStatus = function(status) { + console.debug('Facebook process status >>',status); + + if (status.status === 'connected' && status.authResponse) { + + service.logged = true; + service.error.unknown = false; + service.error.userExists = false; + service.error.login = false; + + service.token = status.authResponse.accessToken; + $.totalStorage('fbtoken', service.token); + + // if the app already has a user, we dont give a crap about facebook + if (App.config.user.id_user) { + return; + } + + if (status.authResponse.userID) { + App.log.account({'userID': status.authResponse.userID}, 'facebook login'); + + // make sure we dont double call the authentication and user creation service + if (!service.running) { + + service.running = true; + App.log.account({'userID': status.authResponse.userID, 'running': service.running}, 'facebook running'); + + // if it is phonegap call a special facebook connection + var data = {}; + if (App.isPhoneGap) { + data.fbtoken = service.token; + } + + // Just call the user api, this will create a facebook user + $http.get(App.service + 'user/facebook', data).success(function(data) { + + App.log.account({'userID': status.authResponse.userID, 'running': service.running, 'data': data }, 'facebook ajax'); + + if (data.error) { + if (data.error == 'facebook id already in use') { + App.log.account({'error': data.error}, 'facebook error'); + service.error.unknown = true; + } + + } else { + service.account.updateInfo(); + service.account.user = data; + + if (service.account.callback) { + service.account.callback(); + service.account.callback = false; + } else { + App.signin.manageLocation(); + try { + $.magnificPopup.close(); + } catch (e) {} + } + + } + + App.log.account({'userID': status.authResponse.userID} , 'facebook currentPage'); + App.rootScope.$broadcast('userAuth', service.user); + }); + } + } + + } else { + // the service is notConnected. reset everything + service.logged = false; + service.error.unknown = false; + service.error.userExists = false; + service.error.login = false; + service.token = null; + $.totalStorage('fbtoken', null); + } + + } + + // sign out of facebook + service.signout = function(callback) { + FB.logout(function() { + service.logged = false; + if (typeof callback === 'function') { callback(); } - ); - } - - service.auth = function( session ){ - - service.logged = true; - - App.log.account( { 'userID' : session.authResponse.userID} , 'facebook login' ); - - if( service.doAuth ){ - - FB.api( '/me', { fields: 'name' }, function( response ) { - - if ( response.error ) { - App.log.account( { 'userID' : session.authResponse.userID, 'error' : response.error } , 'facebook name error' ); - service.error.unknown = true; - return; - } - - App.log.account( { 'userID' : session.authResponse.userID, 'response' : response, 'shouldAuth' : service.doAuth, 'running' : service.running } , 'facebook response' ); - if( response.id ){ - - service.doAuth = false; - - if( !service.running ){ - service.running = true; - App.log.account( { 'userID' : session.authResponse.userID, 'running' : service.running } , 'facebook running' ); - - // Just call the user api, this will create a facebook user - var url = App.service + 'user/facebook'; - - $http( { - method: 'GET', - url: url, - } ).success( function( data ) { - - App.log.account( { 'userID' : session.authResponse.userID, 'running' : service.running, 'data' : data } , 'facebook ajax' ); - - if( data.error ){ - if( data.error == 'facebook id already in use' ){ - // Log the error - App.log.account( { 'error' : data.error } , 'facebook error' ); - service.error.unknown = true; - } - } else { - - service.account.updateInfo(); - service.account.user = data; - if( service.account.callback ){ - service.account.callback(); - service.account.callback = false; - } else { - App.signin.manageLocation(); - $.magnificPopup.close(); - } - - } - App.log.account( { 'userID' : session.authResponse.userID } , 'facebook currentPage' ); - $rootScope.$broadcast( 'userAuth', service.user ); - } ); - } - } else { - service.error.unknown = true; - } - }); - } - } - - service.signout = function( callback ){ - FB.logout( callback() ); - } - - service.startAuth = function( response ){ - service.processStatus( response, service.auth ); - } - - service.processStatus = function( response, callback ){ - if ( response.status === 'connected' && response.authResponse ) { - if( response.authResponse.accessToken ){ - service.logged = true; - service.registerToken( response.authResponse.accessToken ); - } - if( callback ){ - callback( response ); - } - } + }); } return service; - -} ); \ No newline at end of file +}); \ No newline at end of file