Skip to content

Commit

Permalink
allowing session, cache and approval params to be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
ffabreti committed Mar 14, 2016
1 parent 32d8190 commit a605530
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions dist/ng-cordova-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,44 @@ function google($q, $http, $cordovaOauthUtility) {
* @return promise
*/
function oauthGoogle(clientId, appScope, options) {

var deferred = $q.defer();

if(window.cordova) {

if($cordovaOauthUtility.isInAppBrowserInstalled()) {
var redirect_uri = "http://localhost/callback";

var redirect_uri = "http://localhost/callback",
clear_cache = 'no',
clear_session = 'no',
approval_prompt = 'auto';

if(options !== undefined) {
if(options.hasOwnProperty("redirect_uri")) {
redirect_uri = options.redirect_uri;
}
if(options.hasOwnProperty("clear_cache")) {
clear_cache = options.clear_cache === true ? 'yes' : 'no';
}
if(options.hasOwnProperty("clear_session")) {
clear_session = options.clear_session === true ? 'yes' : 'no';
}
if(options.hasOwnProperty("approval_prompt")) {
approval_prompt = options.approval_prompt === true ? 'force' : 'auto';
}
}
var browserRef = window.cordova.InAppBrowser.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=' + redirect_uri + '&scope=' + appScope.join(" ") + '&approval_prompt=force&response_type=token', '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');

var browserRef = window.cordova.InAppBrowser.open(
'https://accounts.google.com/o/oauth2/auth?client_id=' + clientId
+ '&redirect_uri=' + redirect_uri
+ '&scope=' + appScope.join(" ")
+ '&approval_prompt=' + approval_prompt
+ '&response_type=token'
, '_blank'
, 'location=no,clearsessioncache=' + clear_session
+ ',clearcache=' + clear_cache
);

browserRef.addEventListener("loadstart", function(event) {
if((event.url).indexOf(redirect_uri) === 0) {
browserRef.removeEventListener("exit",function(event){});
Expand All @@ -707,15 +735,19 @@ function google($q, $http, $cordovaOauthUtility) {
}
}
});

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

0 comments on commit a605530

Please sign in to comment.