Skip to content

Instantly share code, notes, and snippets.

@thanpolas
Last active December 14, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thanpolas/5155141 to your computer and use it in GitHub Desktop.
Save thanpolas/5155141 to your computer and use it in GitHub Desktop.
<!-- The result of the popup window -->
<script>opener.ss.user.tw.oauthToken('token-312123123');close();</script>
/**
* Perform a login using a popup
*
* @return {when.Promise} a promise.
*/
ssd.user.auth.Twitter.prototype.loginPopup = function() {
var def = when.defer();
// if there was a previous operation, reject it...
if (this._loginOp && this._loginOp.running) {
this._loginOp.reject();
}
// start a new operation
this._loginOp = new ssd.user.auth.twitter.LoginOp({
def: def, timeout: opTimeout
});
open(url, 'popup', 'width=' + width + ',height=' + height);
return def.promise;
};
/**
* This method is called from the popup window after a login operation.
*
* @param {string} token The access token.
*/
ssd.user.auth.Twitter.prototype.oauthToken = function(token) {
this.logger.info('oauthToken() :: Init.');
if (!this._loginOp.running) {
this.logger.severe('oauthToken() :: There is no login operation running!');
// bogus situation... check if there is a promise
if (when.isPromise(this._loginOp.def)) {
// check if the operation has been resolved
if ( /** ????? */ true) {
this._loginOp.reject('bogus');
}
}
// nothing to do
return;
}
this._loginOp.resolve(token);
};
/**
* A login operation object.
*
* @param {Object} params [description]
* @constructor
*/
ssd.user.auth.twitter.LoginOp = function(params) {
/**
* @type {when.Deferred}
*/
this.def = params.def;
/**
* @type {boolean} if a login operation is running.
*/
this.running = true;
this._timeoutIndex = setTimeout(
goog.bind(this.reject, this, 'Operation timeout'),
params.timeout
);
};
/**
* Will reject the login operation.
*
* @param {string=} optErrMsg Define a message for rejecting the deferred.
*/
ssd.user.auth.twitter.LoginOp.prototype.reject = function(optErrMsg) {
if (!this.running) {
return;
}
var errMsg = optErrMsg || 'Canceled by new login';
this.running = false;
clearTimeout(this._timeoutIndex);
this._timeoutIndex = null;
this.def.reject(errMsg);
};
/**
* Will resolve the login operation.
*
* @param {ssd.user.auth.plugin.Response} respObj The response object.
*/
ssd.user.auth.twitter.LoginOp.prototype.resolve = function(respObj) {
if (!this.running) {
return;
}
this.running = false;
clearTimeout(this._timeoutIndex);
this._timeoutIndex = null;
this.def.resolve(respObj);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment