Skip to content

Commit

Permalink
combing and soooothing, fb api works
Browse files Browse the repository at this point in the history
  • Loading branch information
thanpolas committed Mar 9, 2013
1 parent c17e64f commit e6e4503
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 181 deletions.
3 changes: 3 additions & 0 deletions src/core/core.js
Expand Up @@ -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}
Expand Down
51 changes: 49 additions & 2 deletions src/core/response.core.js
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/deps-superstartup.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions src/helpers/helpers.js
Expand Up @@ -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
Expand Down Expand Up @@ -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);
};
81 changes: 52 additions & 29 deletions src/modules/auth/plugins/facebook.auth.js
Expand Up @@ -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
Expand Down Expand Up @@ -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
);
};

Expand Down Expand Up @@ -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;
};
Expand All @@ -464,20 +463,44 @@ 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));
});
};

/**
* 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;
};

/**
Expand Down
32 changes: 23 additions & 9 deletions src/modules/auth/plugins/response.auth-plugins.js
Expand Up @@ -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.
Expand All @@ -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

};
18 changes: 11 additions & 7 deletions src/modules/auth/response.auth.js
Expand Up @@ -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);
Expand All @@ -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


};

0 comments on commit e6e4503

Please sign in to comment.