Skip to content

Commit

Permalink
Add Imgur support
Browse files Browse the repository at this point in the history
  • Loading branch information
nraboy committed Mar 6, 2015
1 parent ca108fa commit 49b997a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ perform tasks when this URL is found.
$cordovaOauth.magento(string baseUrl, string clientId, string clientSecret)
$cordovaOauth.vkontakte(string clientId, array appScope)
$cordovaOauth.adfs(string clientId, string adfsServer, string relyingPartyId)
$cordovaOauth.imgur(string clientId)

Each API call returns a promise. The success callback will provide a response object and the error
callback will return a string.
Expand Down
42 changes: 42 additions & 0 deletions ng-cordova-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* Magento
* vkontakte
* ADFS
* Imgur
*/

(function(){
Expand Down Expand Up @@ -1017,6 +1018,47 @@
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
},

/*
* Sign into the Imgur service
*
* @param string clientId
* @return promise
*/
imgur: function(clientId) {
var deferred = $q.defer();
if(window.cordova) {
var cordovaMetadata = cordova.require("cordova/plugin_list").metadata;
if(cordovaMetadata.hasOwnProperty("org.apache.cordova.inappbrowser") === true) {
var browserRef = window.open('https://api.imgur.com/oauth2/authorize?client_id=' + clientId + '&response_type=token', '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
browserRef.addEventListener('loadstart', function(event) {
if((event.url).indexOf("http://localhost/callback") === 0) {
browserRef.removeEventListener("exit",function(event){});
browserRef.close();
var callbackResponse = (event.url).split("#")[1];
var responseParameters = (callbackResponse).split("&");
var parameterMap = [];
for(var i = 0; i < responseParameters.length; i++) {
parameterMap[responseParameters[i].split("=")[0]] = responseParameters[i].split("=")[1];
}
if(parameterMap.access_token !== undefined && parameterMap.access_token !== null) {
deferred.resolve({ access_token: parameterMap.access_token, expires_in: parameterMap.expires_in, account_username: parameterMap.account_username });
} else {
deferred.reject("Problem authenticating");
}
}
});
browserRef.addEventListener('exit', function(event) {
deferred.reject("The sign in flow was canceled");
});
} else {
deferred.reject("Could not find InAppBrowser plugin");
}
} else {
deferred.reject("Cannot authenticate via a web browser");
}
return deferred.promise;
}

};
Expand Down
Loading

0 comments on commit 49b997a

Please sign in to comment.