Last active
December 14, 2015 21:59
-
-
Save thanpolas/5155141 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- The result of the popup window --> | |
<script>opener.ss.user.tw.oauthToken('token-312123123');close();</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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