@@ -3925,8 +3925,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
39253925 browserRef . close ( ) ;
39263926 }
39273927 } ) ;
3928- browserRef . addEventListener ( 'exit' , function ( event ) {
3929- deferred . reject ( "The sign in flow was canceled" ) ;
3928+ browserRef . addEventListener ( 'exit' , function ( event ) {
3929+ deferred . reject ( "The sign in flow was canceled" ) ;
39303930 } ) ;
39313931 } else {
39323932 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -3964,12 +3964,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
39643964 } )
39653965 . error ( function ( data , status ) {
39663966 deferred . reject ( "Problem authenticating" ) ;
3967- } ) ;
3968- browserRef . close ( ) ;
3967+ } )
3968+ . finally ( function ( ) {
3969+ browserRef . close ( ) ;
3970+ } )
39693971 }
39703972 } ) ;
3971- browserRef . addEventListener ( 'exit' , function ( event ) {
3972- deferred . reject ( "The sign in flow was canceled" ) ;
3973+ browserRef . addEventListener ( 'exit' , function ( event ) {
3974+ deferred . reject ( "The sign in flow was canceled" ) ;
39733975 } ) ;
39743976 } else {
39753977 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4014,8 +4016,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
40144016 browserRef . close ( ) ;
40154017 }
40164018 } ) ;
4017- browserRef . addEventListener ( 'exit' , function ( event ) {
4018- deferred . reject ( "The sign in flow was canceled" ) ;
4019+ browserRef . addEventListener ( 'exit' , function ( event ) {
4020+ deferred . reject ( "The sign in flow was canceled" ) ;
40194021 } ) ;
40204022 } else {
40214023 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4051,12 +4053,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
40514053 } )
40524054 . error ( function ( data , status ) {
40534055 deferred . reject ( "Problem authenticating" ) ;
4056+ } ) .
4057+ finally ( function ( ) {
4058+ browserRef . close ( ) ;
40544059 } ) ;
4055- browserRef . close ( ) ;
40564060 }
40574061 } ) ;
4058- browserRef . addEventListener ( 'exit' , function ( event ) {
4059- deferred . reject ( "The sign in flow was canceled" ) ;
4062+ browserRef . addEventListener ( 'exit' , function ( event ) {
4063+ deferred . reject ( "The sign in flow was canceled" ) ;
40604064 } ) ;
40614065 } else {
40624066 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4100,8 +4104,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
41004104 browserRef . close ( ) ;
41014105 }
41024106 } ) ;
4103- browserRef . addEventListener ( 'exit' , function ( event ) {
4104- deferred . reject ( "The sign in flow was canceled" ) ;
4107+ browserRef . addEventListener ( 'exit' , function ( event ) {
4108+ deferred . reject ( "The sign in flow was canceled" ) ;
41054109 } ) ;
41064110 } else {
41074111 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4141,12 +4145,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
41414145 } )
41424146 . error ( function ( data , status ) {
41434147 deferred . reject ( "Problem authenticating" ) ;
4148+ } )
4149+ . finally ( function ( ) {
4150+ browserRef . close ( ) ;
41444151 } ) ;
4145- browserRef . close ( ) ;
41464152 }
41474153 } ) ;
4148- browserRef . addEventListener ( 'exit' , function ( event ) {
4149- deferred . reject ( "The sign in flow was canceled" ) ;
4154+ browserRef . addEventListener ( 'exit' , function ( event ) {
4155+ deferred . reject ( "The sign in flow was canceled" ) ;
41504156 } ) ;
41514157 } else {
41524158 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4189,8 +4195,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
41894195 browserRef . close ( ) ;
41904196 }
41914197 } ) ;
4192- browserRef . addEventListener ( 'exit' , function ( event ) {
4193- deferred . reject ( "The sign in flow was canceled" ) ;
4198+ browserRef . addEventListener ( 'exit' , function ( event ) {
4199+ deferred . reject ( "The sign in flow was canceled" ) ;
41944200 } ) ;
41954201 } else {
41964202 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4229,12 +4235,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
42294235 } )
42304236 . error ( function ( data , status ) {
42314237 deferred . reject ( "Problem authenticating" ) ;
4238+ } )
4239+ . finally ( function ( ) {
4240+ browserRef . close ( ) ;
42324241 } ) ;
4233- browserRef . close ( ) ;
42344242 }
42354243 } ) ;
4236- browserRef . addEventListener ( 'exit' , function ( event ) {
4237- deferred . reject ( "The sign in flow was canceled" ) ;
4244+ browserRef . addEventListener ( 'exit' , function ( event ) {
4245+ deferred . reject ( "The sign in flow was canceled" ) ;
42384246 } ) ;
42394247 } else {
42404248 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4270,12 +4278,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
42704278 } )
42714279 . error ( function ( data , status ) {
42724280 deferred . reject ( "Problem authenticating" ) ;
4281+ } )
4282+ . finally ( function ( ) {
4283+ browserRef . close ( ) ;
42734284 } ) ;
4274- browserRef . close ( ) ;
42754285 }
42764286 } ) ;
4277- browserRef . addEventListener ( 'exit' , function ( event ) {
4278- deferred . reject ( "The sign in flow was canceled" ) ;
4287+ browserRef . addEventListener ( 'exit' , function ( event ) {
4288+ deferred . reject ( "The sign in flow was canceled" ) ;
42794289 } ) ;
42804290 } else {
42814291 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4351,12 +4361,14 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
43514361 } )
43524362 . error ( function ( error ) {
43534363 deferred . reject ( error ) ;
4364+ } )
4365+ . finally ( function ( ) {
4366+ browserRef . close ( ) ;
43544367 } ) ;
4355- browserRef . close ( ) ;
43564368 }
43574369 } ) ;
4358- browserRef . addEventListener ( 'exit' , function ( event ) {
4359- deferred . reject ( "The sign in flow was canceled" ) ;
4370+ browserRef . addEventListener ( 'exit' , function ( event ) {
4371+ deferred . reject ( "The sign in flow was canceled" ) ;
43604372 } ) ;
43614373 } )
43624374 . error ( function ( error ) {
@@ -4402,8 +4414,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
44024414 browserRef . close ( ) ;
44034415 }
44044416 } ) ;
4405- browserRef . addEventListener ( 'exit' , function ( event ) {
4406- deferred . reject ( "The sign in flow was canceled" ) ;
4417+ browserRef . addEventListener ( 'exit' , function ( event ) {
4418+ deferred . reject ( "The sign in flow was canceled" ) ;
44074419 } ) ;
44084420 } else {
44094421 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4446,8 +4458,8 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
44464458 browserRef . close ( ) ;
44474459 }
44484460 } ) ;
4449- browserRef . addEventListener ( 'exit' , function ( event ) {
4450- deferred . reject ( "The sign in flow was canceled" ) ;
4461+ browserRef . addEventListener ( 'exit' , function ( event ) {
4462+ deferred . reject ( "The sign in flow was canceled" ) ;
44514463 } ) ;
44524464 } else {
44534465 deferred . reject ( "Could not find InAppBrowser plugin" ) ;
@@ -4456,107 +4468,109 @@ angular.module("ngCordova.plugins.oauth", ["ngCordova.plugins.oauthUtility"])
44564468 deferred . reject ( "Cannot authenticate via a web browser" ) ;
44574469 }
44584470 return deferred . promise ;
4459- } ,
4471+ } ,
44604472
4461- /*
4462- * Sign into the Salesforce service
4463- *
4464- * Suggestion: use salesforce oauth with forcetk.js(as SDK)
4465- *
4466- * @param string loginUrl (such as: https://login.salesforce.com ; please notice community login)
4467- * @param string clientId (copy from connection app info)
4468- * @param string redirectUri (callback url in connection app info)
4469- * @return promise
4470- */
4471- salesforce : function ( loginUrl , clientId ) {
4473+ /*
4474+ * Sign into the Salesforce service
4475+ *
4476+ * Suggestion: use salesforce oauth with forcetk.js(as SDK)
4477+ *
4478+ * @param string loginUrl (such as: https://login.salesforce.com ; please notice community login)
4479+ * @param string clientId (copy from connection app info)
4480+ * @param string redirectUri (callback url in connection app info)
4481+ * @return promise
4482+ */
4483+ salesforce : function ( loginUrl , clientId ) {
44724484 var redirectUri = 'http://localhost/callback' ;
44734485 var getAuthorizeUrl = function ( loginUrl , clientId , redirectUri ) {
4474- return loginUrl + 'services/oauth2/authorize?display=touch' +
4475- '&response_type=token&client_id=' + escape ( clientId ) +
4476- '&redirect_uri=' + escape ( redirectUri ) ;
4486+ return loginUrl + 'services/oauth2/authorize?display=touch' +
4487+ '&response_type=token&client_id=' + escape ( clientId ) +
4488+ '&redirect_uri=' + escape ( redirectUri ) ;
44774489 } ;
4478- var startWith = function ( string , str ) {
4479- return ( string . substr ( 0 , str . length ) === str ) ;
4490+ var startWith = function ( string , str ) {
4491+ return ( string . substr ( 0 , str . length ) === str ) ;
44804492 } ;
44814493 var deferred = $q . defer ( ) ;
4482- if ( window . cordova ) {
4483- var cordovaMetadata = cordova . require ( "cordova/plugin_list" ) . metadata ;
4484- if ( cordovaMetadata . hasOwnProperty ( "org.apache.cordova.inappbrowser" ) === true ) {
4485- var browserRef = window . open ( getAuthorizeUrl ( loginUrl , clientId , redirectUri ) , "_blank" , "location=no,clearsessioncache=yes,clearcache=yes" ) ;
4486- browserRef . addEventListener ( "loadstart" , function ( event ) {
4487- if ( startWith ( event . url , redirectUri ) ) {
4488- var oauthResponse = { } ;
4489-
4490- var fragment = ( event . url ) . split ( '#' ) [ 1 ] ;
4491-
4492- if ( fragment ) {
4493- var nvps = fragment . split ( '&' ) ;
4494- for ( var nvp in nvps ) {
4495- var parts = nvps [ nvp ] . split ( '=' ) ;
4496- oauthResponse [ parts [ 0 ] ] = unescape ( parts [ 1 ] ) ;
4497- }
4498- }
4499-
4500- if ( typeof oauthResponse === 'undefined' ||
4501- typeof oauthResponse . access_token === 'undefined' ) {
4502- deferred . reject ( "Problem authenticating" ) ;
4503- } else {
4504- deferred . resolve ( oauthResponse ) ;
4505- }
4506- browserRef . close ( ) ;
4507- }
4508- } ) ;
4509- browserRef . addEventListener ( 'exit' , function ( event ) {
4510- deferred . reject ( "The sign in flow was canceled" ) ;
4511- } ) ;
4512- } else {
4513- deferred . reject ( "Could not find InAppBrowser plugin" ) ;
4494+ if ( window . cordova ) {
4495+ var cordovaMetadata = cordova . require ( "cordova/plugin_list" ) . metadata ;
4496+ if ( cordovaMetadata . hasOwnProperty ( "org.apache.cordova.inappbrowser" ) === true ) {
4497+ var browserRef = window . open ( getAuthorizeUrl ( loginUrl , clientId , redirectUri ) , "_blank" , "location=no,clearsessioncache=yes,clearcache=yes" ) ;
4498+ browserRef . addEventListener ( "loadstart" , function ( event ) {
4499+ if ( startWith ( event . url , redirectUri ) ) {
4500+ var oauthResponse = { } ;
4501+
4502+ var fragment = ( event . url ) . split ( '#' ) [ 1 ] ;
4503+
4504+ if ( fragment ) {
4505+ var nvps = fragment . split ( '&' ) ;
4506+ for ( var nvp in nvps ) {
4507+ var parts = nvps [ nvp ] . split ( '=' ) ;
4508+ oauthResponse [ parts [ 0 ] ] = unescape ( parts [ 1 ] ) ;
4509+ }
45144510 }
4515- } else {
4516- deferred . reject ( "Cannot authenticate via a web browser" ) ;
4517- }
4518- return deferred . promise ;
4519- } ,
45204511
4521- /*
4522- * Sign into the Strava service
4523- *
4524- * @param string clientId
4525- * @param string clientSecret
4526- * @param array appScope
4527- * @return promise
4528- */
4529- strava : function ( clientId , clientSecret , appScope ) {
4530- var deferred = $q . defer ( ) ;
4531- if ( window . cordova ) {
4532- var cordovaMetadata = cordova . require ( "cordova/plugin_list" ) . metadata ;
4533- if ( cordovaMetadata . hasOwnProperty ( "org.apache.cordova.inappbrowser" ) === true ) {
4534- var browserRef = window . open ( 'https://www.strava.com/oauth/authorize?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=' + appScope . join ( "," ) + '&response_type=code&approval_prompt=force' , '_blank' , 'location=no,clearsessioncache=yes,clearcache=yes' ) ;
4535- browserRef . addEventListener ( 'loadstart' , function ( event ) {
4536- if ( ( event . url ) . indexOf ( "http://localhost/callback" ) === 0 ) {
4537- requestToken = ( event . url ) . split ( "code=" ) [ 1 ] ;
4538- $http . defaults . headers . post [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
4539- $http ( { method : "post" , url : "https://www.strava.com/oauth/token" , data : "client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + requestToken } )
4540- . success ( function ( data ) {
4541- deferred . resolve ( data ) ;
4542- } )
4543- . error ( function ( data , status ) {
4544- deferred . reject ( "Problem authenticating" ) ;
4545- } ) ;
4546- browserRef . close ( ) ;
4547- }
4548- } ) ;
4549- browserRef . addEventListener ( 'exit' , function ( event ) {
4550- deferred . reject ( "The sign in flow was canceled" ) ;
4551- } ) ;
4512+ if ( typeof oauthResponse === 'undefined' ||
4513+ typeof oauthResponse . access_token === 'undefined' ) {
4514+ deferred . reject ( "Problem authenticating" ) ;
45524515 } else {
4553- deferred . reject ( "Could not find InAppBrowser plugin" ) ;
4516+ deferred . resolve ( oauthResponse ) ;
45544517 }
4555- } else {
4556- deferred . reject ( "Cannot authenticate via a web browser" ) ;
4557- }
4558- return deferred . promise ;
4518+ browserRef . close ( ) ;
4519+ }
4520+ } ) ;
4521+ browserRef . addEventListener ( 'exit' , function ( event ) {
4522+ deferred . reject ( "The sign in flow was canceled" ) ;
4523+ } ) ;
4524+ } else {
4525+ deferred . reject ( "Could not find InAppBrowser plugin" ) ;
4526+ }
4527+ } else {
4528+ deferred . reject ( "Cannot authenticate via a web browser" ) ;
4529+ }
4530+ return deferred . promise ;
4531+ } ,
4532+
4533+ /*
4534+ * Sign into the Strava service
4535+ *
4536+ * @param string clientId
4537+ * @param string clientSecret
4538+ * @param array appScope
4539+ * @return promise
4540+ */
4541+ strava : function ( clientId , clientSecret , appScope ) {
4542+ var deferred = $q . defer ( ) ;
4543+ if ( window . cordova ) {
4544+ var cordovaMetadata = cordova . require ( "cordova/plugin_list" ) . metadata ;
4545+ if ( cordovaMetadata . hasOwnProperty ( "org.apache.cordova.inappbrowser" ) === true ) {
4546+ var browserRef = window . open ( 'https://www.strava.com/oauth/authorize?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=' + appScope . join ( "," ) + '&response_type=code&approval_prompt=force' , '_blank' , 'location=no,clearsessioncache=yes,clearcache=yes' ) ;
4547+ browserRef . addEventListener ( 'loadstart' , function ( event ) {
4548+ if ( ( event . url ) . indexOf ( "http://localhost/callback" ) === 0 ) {
4549+ requestToken = ( event . url ) . split ( "code=" ) [ 1 ] ;
4550+ $http . defaults . headers . post [ 'Content-Type' ] = 'application/x-www-form-urlencoded' ;
4551+ $http ( { method : "post" , url : "https://www.strava.com/oauth/token" , data : "client_id=" + clientId + "&client_secret=" + clientSecret + "&code=" + requestToken } )
4552+ . success ( function ( data ) {
4553+ deferred . resolve ( data ) ;
4554+ } )
4555+ . error ( function ( data , status ) {
4556+ deferred . reject ( "Problem authenticating" ) ;
4557+ } )
4558+ . finally ( function ( ) {
4559+ browserRef . close ( ) ;
4560+ } ) ;
4561+ }
4562+ } ) ;
4563+ browserRef . addEventListener ( 'exit' , function ( event ) {
4564+ deferred . reject ( "The sign in flow was canceled" ) ;
4565+ } ) ;
4566+ } else {
4567+ deferred . reject ( "Could not find InAppBrowser plugin" ) ;
4568+ }
4569+ } else {
4570+ deferred . reject ( "Cannot authenticate via a web browser" ) ;
45594571 }
4572+ return deferred . promise ;
4573+ }
45604574
45614575 } ;
45624576 } ] ) ;
0 commit comments