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

@ -6,14 +6,9 @@
(function(){
'use strict';
var GitHub,
opener = window.opener;
if (opener) {
GitHub = opener.CloudCmd.GitHub;
GitHub.autorize(GitHub.callback, window.location.search);
window.close();
}
window.addEventListener('message', function(event) {
event.source.postMessage(window.location.search, event.origin);
});
})();
</script>

View file

@ -2,24 +2,21 @@
<html>
<head></head>
<body>
<script src="/cloudcmd/join:lib/promise.js:lib/util.js:lib/client/dom.js:lib/client/events.js:lib/client/load.js:lib/client/files.js:lib/client.js"></script>
<script>
(function() {
"use strict";
var opener = window.opener;
if (opener) {
DOM.Files.get('modules', function(error, modules) {
var Util = opener.Util,
Storage = Util.findObjByNameInArr(modules, 'storage'),
GitHub = Util.findObjByNameInArr(Storage, 'GitHub'),
GitHub_ID = GitHub && GitHub.key;
window.location =
'https://github.com/login/oauth/authorize?client_id=' +
GitHub_ID + '&&scope=repo,user,gist';
});
}
DOM.Files.get('modules', function(error, modules) {
var URL = 'https://github.com/login/oauth/authorize?client_id=',
Storage = Util.findObjByNameInArr(modules, 'storage'),
GitHub = Util.findObjByNameInArr(Storage, 'GitHub'),
GitHub_ID = GitHub && GitHub.key;
window.location =
URL + GitHub_ID + '&&scope=repo,user,gist';
});
})();
</script>
</body>

View file

@ -1449,6 +1449,8 @@ var CloudCmd, Util, DOM, CloudFunc, Dialog;
if (!wnd)
Dialog.alert('Please disable your popup blocker and try again.');
return wnd;
};
/**

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...');
}
});
}
}
});
};