Skip to content

Instantly share code, notes, and snippets.

@battlecow
Created July 10, 2014 02:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save battlecow/8e54e8985d214eaa7a3f to your computer and use it in GitHub Desktop.
Save battlecow/8e54e8985d214eaa7a3f to your computer and use it in GitHub Desktop.
OAuth Plugin
angular.module('resources.OAuth', ['ionic']).
factory('OAuth', ['$q', '$document', '$rootScope', function($q, $document, $rootScope) {
var _pubKey = 'PUB_KEY_HERE';
function isMobile() {
return !!(ionic.Platform.isIOS() || ionic.Platform.isAndroid() || ionic.Platform.isWindowsPhone());
}
var initializeScript = function() {
if (window.OAuth) {
return $q.when(window.OAuth);
}
var script = 'lib/js/oauth.min.js';
if (isMobile()) {
script = 'oauth.js';
}
var d = $q.defer();
function onScriptLoad() {
$rootScope.$apply(function() {
window.OAuth.initialize(_pubKey);
d.resolve(window.OAuth);
});
}
var scriptTag = $document[0].createElement('script');
scriptTag.type = 'text/javascript';
scriptTag.async = true;
scriptTag.src = script;
scriptTag.onreadystatechange = function() {
if (this.readyState == 'complete')
onScriptLoad();
}
scriptTag.onload = onScriptLoad;
var s = $document[0].getElementsByTagName('body')[0];
s.appendChild(scriptTag);
return d.promise;
};
return {
service: function() {
if (window.OAuth) {
return $q.when(window.OAuth);
} else {
return initializeScript();
}
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment