refactor(github) auth

This commit is contained in:
coderaiser 2014-12-08 03:53:46 -05:00
parent 6d84f734d2
commit 8ac1cc5837
4 changed files with 36 additions and 29 deletions

View file

@ -7,8 +7,11 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
function GitHubProto(callback) {
var GitHub = this,
Storage = DOM.Storage,
Popup,
Storage = DOM.Storage,
Events = DOM.Events,
GH,
User;
@ -22,6 +25,13 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
Util.exec.ret(callback)
]);
Events.add('message', function(event) {
if (event.origin === document.location.origin) {
Popup.close();
GitHub.autorize(event.data);
}
});
GitHub.callback = function() {
Util.exec.series([
GitHub.getUserData,
@ -31,8 +41,8 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
}
function load(callback) {
var dir = CloudCmd.LIBDIRCLIENT + 'storage/github/',
url = join([
var dir = CloudCmd.LIBDIRCLIENT + 'storage/github/',
url = CloudCmd.join([
dir + 'lib/underscore.js',
dir + 'lib/base64.js',
dir + 'github.js'
@ -51,7 +61,7 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
GitHub.autorize = function(callback, code) {
Storage.get('token', function(error, token) {
var isContain,
var isContain, popup,
apiURL = CloudFunc.apiURL,
URL = '//' + window.location.host + '/auth/github';
@ -64,9 +74,11 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
isContain = ~code.indexOf('?code=');
if (!isContain)
DOM.openWindow(URL);
else
if (!isContain) {
Popup = DOM.openWindow(URL);
if (Popup)
Popup.postMessage('', window.location.origin);
} else {
DOM.load.ajax({
type : 'put',
url : apiURL + '/auth',
@ -82,6 +94,7 @@ var CloudCmd, Util, join, DOM, CloudFunc, Github, cb;
Util.log('Worning: token not getted...');
}
});
}
}
});
};