From e6e450398330de79fc6d5f000c31a1e41bc8a30a Mon Sep 17 00:00:00 2001 From: Thanasis Polychronakis Date: Sat, 9 Mar 2013 21:44:11 +0200 Subject: [PATCH] combing and soooothing, fb api works --- src/core/core.js | 3 + src/core/response.core.js | 51 +++++++- src/deps-superstartup.js | 4 +- src/helpers/helpers.js | 15 +-- src/modules/auth/plugins/facebook.auth.js | 81 +++++++----- .../auth/plugins/response.auth-plugins.js | 32 +++-- src/modules/auth/response.auth.js | 18 +-- src/modules/auth/user.auth.login.js | 30 ++--- src/modules/auth/user.auth.model.js | 116 ++++++++++-------- src/network/{response.js => response.sync.js} | 17 ++- test/bdd/deps-test-bdd.js | 2 +- test/bdd/index.html | 3 +- test/bdd/main.js | 7 +- .../userAuth/plugins/facebook.auth.test.js | 10 +- .../userAuth/plugins/pluginInterface.test.js | 20 ++- .../modules/userAuth/userAuth.events.test.js | 73 +++++++---- .../modules/userAuth/userAuthLogin.test.js | 8 +- .../modules/userAuth/userAuthLogout.test.js | 32 ++++- test/fixtures/userAuth.facebook.fixture.js | 2 +- test/unit/deps-test-tdd.js | 2 +- 20 files changed, 345 insertions(+), 181 deletions(-) rename src/network/{response.js => response.sync.js} (61%) diff --git a/src/core/core.js b/src/core/core.js index 92b41b6..17b23c2 100644 --- a/src/core/core.js +++ b/src/core/core.js @@ -119,6 +119,9 @@ goog.inherits(ssd.Core, ssd.Module); ssd.Core.prototype.logger = goog.debug.Logger.getLogger('ssd.Core'); +ssd.Core.prototype.log = goog.debug.Logger.getLogger('myapp'); + + /** * Events triggered by core * @enum {string} diff --git a/src/core/response.core.js b/src/core/response.core.js index 9ec3c79..078b7c2 100644 --- a/src/core/response.core.js +++ b/src/core/response.core.js @@ -10,14 +10,61 @@ goog.require('goog.events.Event'); * The response data object used globaly by superstartup. * * + * @param {ssd.Response=} optResp Another response object to augment. + * @param {Array.Object=} optChilds An array of object with keys to use for the + * response Object. * @constructor */ -ssd.Response = function() { - goog.object.map(ssd.response, function(el, ind){ +ssd.Response = function( optResp, optChilds ) { + var childs = optChilds || []; + + this._copyDefaults( ssd.response ); + // if childs are there then the first item is the top-most child, + // so we want to start from the bottom up + var len = childs.length; + while(len--) { + this._copyDefaults( childs[len] ); + } + + if (optResp && optResp.extend) { + optResp.extend(this); + } + +}; + +/** + * Copy the default key/value pairs from the provided object. + * + * @param {Object} obj the object with the default values. + * @private + */ +ssd.Response.prototype._copyDefaults = function(obj) { + goog.object.map( obj , function(el, ind){ this[ind] = el; }, this); }; +/** + * Will augment the provided response object with the values of this one. + * + * The extending resp obj overwrites values of the provided one. + * + * @param {ssd.Response} inst A response object instance to extend. + * @return {ssd.Response} The augmented resp object although you may use the + * resp object provided directly as it is not cloned. + */ +ssd.Response.prototype.extend = function( inst ) { + var clonedSelf = goog.object.clone( this ); + // remove methods + delete clonedSelf.extend; + delete clonedSelf.event; + delete clonedSelf._copyDefaults; + + goog.object.extend( inst, clonedSelf ); + + return inst; +}; + /** * get the response object augmented by an event object. * diff --git a/src/deps-superstartup.js b/src/deps-superstartup.js index e7bb93c..220afe2 100644 --- a/src/deps-superstartup.js +++ b/src/deps-superstartup.js @@ -26,7 +26,7 @@ goog.addDependency('../../../src/modules/auth/user.auth.config.js', ['ssd.user.a goog.addDependency('../../../src/modules/auth/user.auth.controller.js', ['ssd.user.Auth', 'ssd.user.Auth.Error'], ['ssd.invocator', 'ssd.register', 'ssd.structs.DynamicMap', 'ssd.structs.Map', 'ssd.user.AuthLogin', 'ssd.user.AuthModel', 'ssd.user.OwnItem', 'ssd.user.auth.EventType', 'ssd.user.auth.config', 'ssd.user.types']); goog.addDependency('../../../src/modules/auth/user.auth.events.js', ['ssd.user.auth.EventType'], []); goog.addDependency('../../../src/modules/auth/user.auth.login.js', ['ssd.user.AuthLogin'], ['goog.dom', 'goog.dom.forms', 'ssd.helpers', 'ssd.sync', 'ssd.user.AuthModel', 'ssd.user.auth.EventType', 'ssd.user.auth.config']); -goog.addDependency('../../../src/modules/auth/user.auth.model.js', ['ssd.user.AuthModel'], ['goog.json', 'ssd.Module', 'ssd.core.config', 'ssd.sync', 'ssd.user.auth.EventType', 'ssd.user.auth.config']); +goog.addDependency('../../../src/modules/auth/user.auth.model.js', ['ssd.user.AuthModel'], ['goog.json', 'ssd.Module', 'ssd.core.config', 'ssd.sync', 'ssd.user.auth.EventType', 'ssd.user.auth.Response', 'ssd.user.auth.config']); goog.addDependency('../../../src/modules/metrics/googleAnalytics.js', ['ssd.metrics.ga'], []); goog.addDependency('../../../src/modules/metrics/metrics.js', ['ssd.metrics'], ['ssd.metrics.ga', 'ssd.metrics.mixpanel']); goog.addDependency('../../../src/modules/metrics/mixpanel.js', ['ssd.metrics.mixpanel'], []); @@ -35,7 +35,7 @@ goog.addDependency('../../../src/modules/user/user.item.js', ['ssd.user.Item', ' goog.addDependency('../../../src/modules/user/user.ownItem.js', ['ssd.user.OwnItem'], ['ssd.user.Item', 'ssd.user.auth.EventType']); goog.addDependency('../../../src/modules/user/user.types.js', ['ssd.user.types'], ['ssd.structs.Map']); goog.addDependency('../../../src/network/ajax.js', ['ssd.ajax', 'ssd.ajax.Method'], ['goog.net.XhrIo', 'ssd.sync.Response']); -goog.addDependency('../../../src/network/response.js', ['ssd.sync.Response'], ['goog.object', 'ssd.Response']); +goog.addDependency('../../../src/network/response.sync.js', ['ssd.sync.Response'], ['goog.object', 'ssd.Response']); goog.addDependency('../../../src/network/sync.js', ['ssd.sync'], ['ssd.ajax', 'ssd.sync.Response']); goog.addDependency('../../../src/structs/dynamicLinkedMap.js', ['ssd.structs.DynamicLinkedMap', 'ssd.structs.DynamicLinkedMap.EventType'], ['goog.events.EventTarget', 'goog.object', 'ssd.structs.DynamicMap', 'ssd.structs.LinkedMap']); goog.addDependency('../../../src/structs/dynamicMap.js', ['ssd.structs.DynamicMap', 'ssd.structs.DynamicMap.EventType', 'ssd.structs.DynamicMap.Operation'], ['goog.events.EventTarget', 'goog.object', 'ssd.structs.Map']); diff --git a/src/helpers/helpers.js b/src/helpers/helpers.js index f7cd247..ac4caca 100644 --- a/src/helpers/helpers.js +++ b/src/helpers/helpers.js @@ -14,7 +14,7 @@ goog.require('goog.json'); ssd.noop = function(){}; /** @const {string} the back pipe key */ -ssd.BACKPIPE_KEY = '__backPipe'; +ssd.BACKPIPE_KEY = 'backPipe'; /** * Will add a key in the eventObj that is a function @@ -298,10 +298,11 @@ ssd.cb2promise = function(defer, cb, optSelf) { * @return {ssd.sync.Response} */ ssd._getResponse = function (responseRaw) { - return { - httpStatus: 200, - success: true, - responseRaw: goog.json.serialize(responseRaw), - errorMessage: null - }; + var syncResp = new ssd.sync.Response(); + syncResp.httpStatus= 200; + syncResp.success= true; + syncResp.responseRaw= goog.json.serialize(responseRaw); + syncResp.errorMessage= null; + + return when.resolve(syncResp); }; diff --git a/src/modules/auth/plugins/facebook.auth.js b/src/modules/auth/plugins/facebook.auth.js index 483cd92..fec4a48 100644 --- a/src/modules/auth/plugins/facebook.auth.js +++ b/src/modules/auth/plugins/facebook.auth.js @@ -38,6 +38,12 @@ ssd.user.auth.Facebook = function( authInst ) { // synchronously this.config(ssd.user.auth.config.Key.FB_LOAD_API, true); + /** + * @type {?Object} udo as provided by facebook + * @private + */ + this._fbUdo = null; + /** * @type {string} * @private @@ -343,31 +349,27 @@ ssd.user.auth.Facebook.prototype.login = function(optCb, optSelf) { * @param {!Function} cb a function. * @param {Object|undefined} self scope. * @param {boolean} status operation status. - * @param {ssd.user.auth.plugin.Response|string=} optResObj response object or + * @param {ssd.user.auth.plugin.Response|string=} optRespObj response object or * error message. - * @param {ssd.user.auth.Response=} optAuthResObj The response object from - * auth class. * @private */ ssd.user.auth.Facebook.prototype._promise2cb = function(cb, self, status, - optResObj, optAuthResObj) { + optRespObj) { this.logger.finer('_promise2cb() :: Init.'); - var resObj = optResObj || {}; + var respObj = optRespObj || {}; if (!status) { - cb.call(self, resObj, this._auth.isAuthed()); + cb.call(self, respObj, this._auth.isAuthed()); return; } - var authResObj = optAuthResObj || {}; - cb.call(self, null, this._auth.isAuthed(), this._auth.getSet(), - authResObj.rawServer, - resObj.rawThirdParty + respObj.serverRaw, + respObj.responsePluginRaw ); }; @@ -415,43 +417,40 @@ ssd.user.auth.Facebook.prototype._isAuthedFromResponse = function(response) { this.logger.info('_isAuthedFromResponse() :: Init.'); var def = when.defer(); - var resObj = new ssd.user.auth.plugin.Response(); + var respObj = new ssd.user.auth.plugin.Response(); if ( !goog.isObject(response)) { - this.logger.warning('_isAuthedFromResponse() :: response not object:' + response); + this.logger.warning('_isAuthedFromResponse() :: response not object: ' + response); return def.reject('response not an object'); } var isAuthed = 'connected' === response['status']; - resObj.rawThirdParty = response; - resObj.authState = isAuthed; - + respObj.responsePluginRaw = response; + respObj.authState = isAuthed; // check if the response received differs from our stored state if (isAuthed === this._isAuthed) { this.logger.fine('New auth state same as old: ' + isAuthed); - return def.resolve(resObj); + return def.resolve(respObj); } this._isAuthed = isAuthed; // only dispatch EXT_AUTH_CHANGE events AFTER initial auth response if (!this._gotInitialResponse) { - this.logger.fine('Initial auth response not received yet'); - return def.resolve(resObj); + this.logger.fine('Initial auth response not received yet.'); + return def.resolve(respObj); } this.logger.info('_isAuthedFromResponse() :: Auth state changed to: ' + isAuthed); - var eventObj = resObj.event(ssd.user.auth.EventType.EXT_AUTH_CHANGE, this); - - // add a backpipe so that auth lib will pass back - // a promise. + var eventObj = respObj.event(ssd.user.auth.EventType.EXT_AUTH_CHANGE, this); + // add a backpipe so that auth lib will pass back a promise. var backPipe = ssd.eventBackPipe( eventObj, when.defer() ); - this.dispatchEvent(eventObj); + this.dispatchEvent( eventObj ); - backPipe().then( goog.partial(def.resolve, response), def.reject); + backPipe().then( def.resolve, def.reject ); return def; }; @@ -464,6 +463,7 @@ ssd.user.auth.Facebook.prototype._isAuthedFromResponse = function(response) { ssd.user.auth.Facebook.prototype.logout = function() { this.logger.info('logout() :: Init'); this._isAuthed = false; + this._fbUdo = null; this.dispatchEvent(ssd.user.auth.EventType.EXT_AUTH_CHANGE); FB.logout(function(response) { this.logger.info('logout() :: callback. Deep expose of response:' + goog.debug.deepExpose(response, false, true)); @@ -471,13 +471,36 @@ ssd.user.auth.Facebook.prototype.logout = function() { }; /** - * If user is authed returns us a {@link ssd.user.types.extSource} - * data object - * @return {ssd.user.types.extSource|null} null if not authed + * If user is authed returns the user data object as provided by facebook. + * + * @param {Function=} optCb Callback. + * @param {Object} optSelf scope for cb. + * @return {when.Promise} null if not authed. */ -ssd.user.auth.Facebook.prototype.getUser = function() { +ssd.user.auth.Facebook.prototype.getUser = function(optCb, optSelf) { + var def = when.defer(); - return null; + var cb = optCb || ssd.noop; + + def.promise.then(goog.bind(cb, optSelf), goog.bind(cb, optSelf)); + + if ( !this.isAuthed() ) { + return def.resolve(null); + } + + if (this._fbUdo) { + return def.resolve( this._fbUdo ); + } + FB.api('/me', goog.bind(function(response) { + if ( goog.isObject( response )) { + this._fbUdo = response; + def.resolve(response); + } else { + def.reject( response ); + } + }, this)); + + return def.promise; }; /** diff --git a/src/modules/auth/plugins/response.auth-plugins.js b/src/modules/auth/plugins/response.auth-plugins.js index ba17d1f..13a128f 100644 --- a/src/modules/auth/plugins/response.auth-plugins.js +++ b/src/modules/auth/plugins/response.auth-plugins.js @@ -9,20 +9,34 @@ goog.require('ssd.user.auth.Response'); /** * Defines the response object that will be passed on ajax.send callbaks. * - * + * @param {ssd.Response=} optResp Another response object to augment. + * @param {Array.Object=} optChilds An array of object with keys to use for the + * response Object. * @constructor * @extends {ssd.user.auth.Response} */ -ssd.user.auth.plugin.Response = function() { - goog.base(this); - - goog.object.map(ssd.sync.response, function(el, ind){ - this[ind] = el; - }, this); - +ssd.user.auth.plugin.Response = function( optResp, optChilds ) { + var childs = optChilds || []; + childs.push( ssd.user.auth.plugin.response ); + goog.base(this, optResp, childs); }; goog.inherits( ssd.user.auth.plugin.Response, ssd.user.auth.Response); +/** + * Will check if type type of inst is an event and only pick + * the keys of this class. + * + * @param {ssd.Response | goog.events.Event} inst A response object instance to extend. + * @return {ssd.Response} The augmented resp object although you may use the + * resp object provided directly as it is not cloned. + */ +ssd.user.auth.plugin.Response.prototype.extend = function( inst ) { + if ( inst instanceof goog.events.Event ) { + this.responsePluginRaw = inst.responsePluginRaw; + return this; + } + goog.base(this, 'extend', inst); +}; /** * The response object. @@ -32,6 +46,6 @@ goog.inherits( ssd.user.auth.plugin.Response, ssd.user.auth.Response); ssd.user.auth.plugin.response = { /** @type {?Object|string} The raw response from the third-party API */ - rawThirdParty: null + responsePluginRaw: null }; diff --git a/src/modules/auth/response.auth.js b/src/modules/auth/response.auth.js index 82980f2..45f9f23 100644 --- a/src/modules/auth/response.auth.js +++ b/src/modules/auth/response.auth.js @@ -10,15 +10,16 @@ goog.require('ssd.sync.Response'); * Defines the response object that will be passed on ajax.send callbaks. * * + * @param {ssd.Response=} optResp Another response object to augment. + * @param {Array.Object=} optChilds An array of object with keys to use for the + * response Object. * @constructor * @extends {ssd.sync.Response} */ -ssd.user.auth.Response = function() { - goog.base(this); - - goog.object.map(ssd.sync.response, function(el, ind){ - this[ind] = el; - }, this); +ssd.user.auth.Response = function( optResp, optChilds ) { + var childs = optChilds || []; + childs.push( ssd.user.auth.response ); + goog.base(this, optResp, childs); }; goog.inherits( ssd.user.auth.Response, ssd.sync.Response); @@ -34,8 +35,11 @@ ssd.user.auth.response = { /** @type {boolean} The current authentication state */ authState: false, + /** @type {Object} User data Object */ + udo: null, + /** @type {?Object|string} The raw response from the server */ - rawServer: null + serverRaw: null }; diff --git a/src/modules/auth/user.auth.login.js b/src/modules/auth/user.auth.login.js index 6d252d3..942319a 100644 --- a/src/modules/auth/user.auth.login.js +++ b/src/modules/auth/user.auth.login.js @@ -149,8 +149,8 @@ ssd.user.AuthLogin.prototype.logout = function(optCb, optSelfObj) { var cb = optCb || ssd.noop; - def.promise.then( goog.bind( function( success ) { - cb.call(optSelfObj, null, success ); + def.promise.then( goog.bind( function( respObj ) { + cb.call(optSelfObj, null, respObj.success ); }, this )); def.promise.otherwise( goog.bind( function( errMsg ) { cb.call(optSelfObj, errMsg, false ); @@ -172,15 +172,11 @@ ssd.user.AuthLogin.prototype.logout = function(optCb, optSelfObj) { // perform deauthentication this._doAuth(false); - var respCb = goog.bind(function() { - when.chain( - this._logoutResponse.apply(this, arguments), - def.resolver - ); - }, this); + var logoutUrl = this.config(ssd.user.auth.config.Key.LOGOUT_URL); - ssd.sync.send( this.config(ssd.user.auth.config.Key.LOGOUT_URL), respCb, - ssd.ajax.Method.POST); + ssd.sync.send( logoutUrl, null, ssd.ajax.Method.POST ) + .then( goog.bind(this._logoutResponse, this), def.reject ) + .then( def.resolve, def.reject ); return def.promise; }; @@ -198,25 +194,19 @@ ssd.user.AuthLogin.prototype._logoutResponse = function( response ) { var def = when.defer(); - var eventObj = { - type: ssd.user.auth.EventType.ON_LOGOUT_RESPONSE, - 'responseRaw': response.responseRaw, - 'httpStatus': response.httpStatus, - 'ajaxStatus': response.success, - 'authState': false, - 'errorMessage': response.errorMessage - }; + var respObj = new ssd.user.auth.Response(response); + var eventObj = respObj.event(ssd.user.auth.EventType.ON_LOGOUT_RESPONSE, this); // dispatch event and check if don't want exec. if ( false === this.dispatchEvent(eventObj) ) { this.logger.info('_logoutResponse() :: canceled due to ' + 'event preventDefault'); - return def.resolve(true); + return def.resolve(respObj); } // switch event type eventObj.type = ssd.user.auth.EventType.AFTER_LOGOUT_RESPONSE; this.dispatchEvent(eventObj); - return def.resolve(true); + return def.resolve(respObj); }; diff --git a/src/modules/auth/user.auth.model.js b/src/modules/auth/user.auth.model.js index e5970ca..9c594a3 100644 --- a/src/modules/auth/user.auth.model.js +++ b/src/modules/auth/user.auth.model.js @@ -9,6 +9,7 @@ goog.provide('ssd.user.AuthModel'); goog.require('goog.json'); goog.require('ssd.core.config'); +goog.require('ssd.user.auth.Response'); goog.require('ssd.user.auth.EventType'); goog.require('ssd.user.auth.config'); goog.require('ssd.Module'); @@ -61,6 +62,14 @@ ssd.user.AuthModel = function() { * ssd.user.Auth.SourceItem>} */ this._mapSources = new ssd.structs.Map(); + + /** + * An array containing the id's of all the ext sources that are currently + * authenticated. + * @type {Array.} + */ + this._authedSources = []; + }; goog.inherits( ssd.user.AuthModel, ssd.Module); @@ -152,7 +161,7 @@ ssd.user.AuthModel.prototype._authChange = function( ev ) { }); } - // check if in our authed map + // get saved auth state from the sources map var extItem = this._mapSources.get(plugin.SOURCEID); var isExtAuthed = extItem.isAuthed; @@ -169,13 +178,26 @@ ssd.user.AuthModel.prototype._authChange = function( ev ) { this._mapSources.set(plugin.SOURCEID, extItem); var promise; - if (isExtAuthed) { + if ( extItem.isAuthed ) { + if ( -1 === this._authedSources.indexOf(plugin.SOURCEID) ) { + this._authedSources.push( plugin.SOURCEID ); + } promise = this.verifyExtAuthWithLocal(plugin.SOURCEID); } else { + var pluginIndex = this._authedSources.indexOf(plugin.SOURCEID); + if ( 0 <= pluginIndex) { + this._authedSources.splice(pluginIndex, 1); + } promise = this._checkAuthState(); } - def.resolver.resolve(promise); + // when response comes back, extend it with the properties of the event obj + // (for now just passes responsePluginRaw prop) + promise.then(function(response){ + var respObj = new ssd.user.auth.plugin.Response( response ); + respObj.extend(ev); + def.resolve(respObj); + }, def.reject); }; @@ -223,13 +245,13 @@ ssd.user.AuthModel.prototype.verifyExtAuthWithLocal = function( sourceId ) { var def = when.defer(); this.logger.info('_verifyExtAuthWithLocal() :: Init. LocalAuth Switch:' + - ' sourceId :' + sourceId ); + ' sourceId: ' + sourceId ); // get plugin item var extItem = this._mapSources.get(sourceId); // get plugin instance - var extInst =extItem.inst; + var extInst = extItem.inst; // dispatch event and check for cancel... var eventObj = { @@ -250,25 +272,23 @@ ssd.user.AuthModel.prototype.verifyExtAuthWithLocal = function( sourceId ) { // No verification with server is allowed by config. // authenticate the user. - this._doAuth( true ); - return def.resolve( true ); + return this._doAuth( true ); } // // Prepare the ajax call // // get local auth url from ext plugin or use default one. - var url = extInst.config( ssd.user.auth.Key.EXT_SOURCES_AUTH_URL ) || - this.config( ssd.user.auth.Key.LOGIN_URL ); + var url = extInst.config( ssd.user.auth.config.Key.EXT_SOURCES_AUTH_URL ) || + this.config( ssd.user.auth.config.Key.LOGIN_URL ); var data = {}, - paramSource = this.config( ssd.user.auth.Key.PARAM_SOURCE_ID ), - paramAccessToken = this.config( ssd.user.auth.Key.PARAM_ACCESS_TOKEN ); + paramSource = this.config( ssd.user.auth.config.Key.PARAM_SOURCE_ID ), + paramAccessToken = this.config( ssd.user.auth.config.Key.PARAM_ACCESS_TOKEN ); data[paramSource] = sourceId; data[paramAccessToken] = extInst.getAccessToken(); - return this.performLocalAuth( url, data ); }; @@ -315,9 +335,10 @@ ssd.user.AuthModel.prototype.performLocalAuth = function( url, data ) { data = backPipe(); - var cb = ssd.cb2promise(def, this._serverAuthResponse, this); - - ssd.sync.send( url, cb, ssd.ajax.Method.POST, data ); + this.logger.fine('performLocalAuth() :: sending request...'); + ssd.sync.send( url, null, ssd.ajax.Method.POST, data ) + .then(goog.bind(this._serverAuthResponse, this), def.reject) + .then(def.resolve, def.reject); return def.promise; }; @@ -334,7 +355,7 @@ ssd.user.AuthModel.prototype.performLocalAuth = function( url, data ) { * 1. The operation succeeded * 2. We received a positive or negative response from the server * - * @param {ssd.sync.ResponseObject} response The response object. + * @param {ssd.sync.Response} response The response object. * @return {when.Promise} A promise. * @private */ @@ -343,25 +364,10 @@ ssd.user.AuthModel.prototype._serverAuthResponse = function( response ) { this.logger.info('_serverAuthResponse() :: Init'); - 'onetotrack'; - // - // - // - // Make this object a typed response object both for events and promises. - // - // All auth class promises should return this object - // - // - // - // - var eventObj = { - type: ssd.user.auth.EventType.ON_LOGIN_RESPONSE, - 'responseRaw': response.responseRaw, - 'httpStatus': response.httpStatus, - 'ajaxStatus': response.success, - 'authState': false, - 'errorMessage': response.errorMessage - }; + var respObj = new ssd.user.auth.Response( response ); + respObj.authState = this.isAuthed(); + + var eventObj = respObj.event(ssd.user.auth.EventType.ON_LOGIN_RESPONSE, this); // dispatch event and check if don't want exec. if ( false === this.dispatchEvent(eventObj) ) { @@ -389,9 +395,9 @@ ssd.user.AuthModel.prototype._serverAuthResponse = function( response ) { } catch(ex) { this.logger.warning('_serverAuthResponse() :: response failed' + ' to parse as JSON'); - eventObj['errorMessage'] = 'response not JSON'; + eventObj.errorMessage = 'response not JSON'; this.dispatchEvent(eventObj); - return def.reject('resp not JSON'); + return def.reject(eventObj.errorMessage); } // check if status check is enabled. @@ -403,9 +409,9 @@ ssd.user.AuthModel.prototype._serverAuthResponse = function( response ) { if (valuator !== responseParsed[statusKey]) { // operation has failed... this.logger.warning('_serverAuthResponse() :: operation got a false response'); - eventObj['errorMessage'] = 'status failed'; + respObj.errorMessage = eventObj.errorMessage = 'status failed'; this.dispatchEvent(eventObj); - return def.resolve( false ); + return def.resolve( respObj ); } } @@ -424,22 +430,22 @@ ssd.user.AuthModel.prototype._serverAuthResponse = function( response ) { responseParsed = udo = response.responseRaw; } + respObj.udo = udo; + respObj.serverRaw = responseParsed; + // auth method will also validate. if ( !this.auth(udo) ) { - eventObj['errorMessage'] = 'user data object not valid'; + eventObj.errorMessage = 'user data object not valid'; this.dispatchEvent(eventObj); return def.reject('udo not valid'); } - eventObj['udo'] = udo; - eventObj['authState'] = true; + respObj.authState = true; + eventObj = respObj.event(ssd.user.auth.EventType.AFTER_LOGIN_RESPONSE, this); this.dispatchEvent(eventObj); - return def.resolve({ - 'authState': true, - 'udo': udo, - 'response': response.responseRaw - }); + + return def.resolve(respObj); }; /** @@ -473,8 +479,6 @@ ssd.user.AuthModel.prototype.deAuth = function() { this._doAuth( false ); }; - - /** * Perform an auth or deauth based on parameter * @@ -497,9 +501,21 @@ ssd.user.AuthModel.prototype._doAuth = function (isAuthed) { type: ssd.user.auth.EventType.AUTH_CHANGE }; this.dispatchEvent(eventObj); - return when.defer().resolve(isAuthed); + return when.resolve(isAuthed); }; +/** + * Return an array of string with the id's of external sources + * that are authenticated. + * + * @return {Array.} An array of strings. + */ +ssd.user.AuthModel.prototype.authedSources = function() { + return Array.prototype.slice.call(this._authedSources, 0); +}; + + + /** * If current user is authenticated * diff --git a/src/network/response.js b/src/network/response.sync.js similarity index 61% rename from src/network/response.js rename to src/network/response.sync.js index cc01fe7..a8b0875 100644 --- a/src/network/response.js +++ b/src/network/response.sync.js @@ -9,17 +9,16 @@ goog.require('ssd.Response'); /** * Defines the response object that will be passed on ajax.send callbaks. * - * + * @param {ssd.Response=} optResp Another response object to augment. + * @param {Array.Object=} optChilds An array of object with keys to use for the + * response Object. * @constructor * @extends {ssd.Response} */ -ssd.sync.Response = function() { - goog.base(this); - - goog.object.map(ssd.sync.response, function(el, ind){ - this[ind] = el; - }, this); - +ssd.sync.Response = function( optResp, optChilds ) { + var childs = optChilds || []; + childs.push( ssd.sync.response ); + goog.base(this, optResp, childs); }; goog.inherits( ssd.sync.Response, ssd.Response); @@ -35,7 +34,7 @@ ssd.sync.response = { httpStatus: null, /** @type {?string} */ - rawResponse: null, + responseRaw: null, /** @type {?goog.net.XhrIo} */ xhr: null diff --git a/test/bdd/deps-test-bdd.js b/test/bdd/deps-test-bdd.js index 009418b..7edb0a7 100644 --- a/test/bdd/deps-test-bdd.js +++ b/test/bdd/deps-test-bdd.js @@ -1,6 +1,6 @@ // This file was autogenerated by closure-library//closure/bin/build/depswriter.py. // Please do not edit. -goog.addDependency('../../../../../test/bdd/bootstrap.js', ['ssd.test.bootstrap'], ['ssd.test.fixture.userOne', 'ssd.test.main']); +goog.addDependency('../../../../../test/bdd/bootstrap.js', ['ssd.test.bootstrap'], ['ssd.test.core', 'ssd.test.event.api', 'ssd.test.fixture.userOne', 'ssd.test.main', 'ssd.test.userAuth.core', 'ssd.test.userAuth.facebook', 'ssd.test.userAuth.login', 'ssd.test.userAuth.logout']); goog.addDependency('../../../../../test/bdd/core/core.test.js', ['ssd.test.core'], ['ssd.test.fixture.event']); goog.addDependency('../../../../../test/bdd/events/eventsAPI.test.js', ['ssd.test.event.api'], []); goog.addDependency('../../../../../test/bdd/main.js', ['ssd.test.main'], ['ssd.test.fixture.auth.fb', 'ssd.test.fixture.userOne']); diff --git a/test/bdd/index.html b/test/bdd/index.html index 8804c14..1455504 100644 --- a/test/bdd/index.html +++ b/test/bdd/index.html @@ -31,6 +31,7 @@