Created
July 10, 2014 02:30
-
-
Save battlecow/8e54e8985d214eaa7a3f to your computer and use it in GitHub Desktop.
OAuth Plugin
This file contains hidden or 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
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