diff --git a/src/oauth.ts b/src/oauth.ts index 70f4b450..7f7e874e 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -6,16 +6,17 @@ import OAuth2 from './oauth2'; export default class OAuth { static $inject = ['$http', 'SatellizerConfig', 'SatellizerShared', 'SatellizerOAuth1', 'SatellizerOAuth2']; - + constructor(private $http: angular.IHttpService, + private $q: angular.IQService, private SatellizerConfig: Config, private SatellizerShared: Shared, private SatellizerOAuth1: OAuth1, private SatellizerOAuth2: OAuth2) { } - authenticate(name: string, userData: any): Promise { - return new Promise((resolve, reject) => { + authenticate(name: string, userData: any): angular.IPromise { + return this.$q((resolve, reject) => { const provider = this.SatellizerConfig.providers[name]; const initialize: any = provider.oauthType === '1.0' ? this.SatellizerOAuth1.init(provider, userData) : this.SatellizerOAuth2.init(provider, userData); diff --git a/src/oauth1.ts b/src/oauth1.ts index b38edb47..4b7980f3 100644 --- a/src/oauth1.ts +++ b/src/oauth1.ts @@ -6,7 +6,7 @@ import { IOAuth1Options } from './oauth1'; export interface IOAuth1 { init(options: any, data: any): angular.IPromise; getRequestToken(): angular.IHttpPromise; - openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg): Promise; + openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg): angular.IPromise; exchangeForToken(oauthData: any, userData: any): angular.IHttpPromise; buildQueryString(obj: any): string; } @@ -63,7 +63,7 @@ export default class OAuth1 implements IOAuth1 { }); } - openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg): Promise { + openPopup(options: IOAuth1Options, response: angular.IHttpPromiseCallbackArg): angular.IPromise { const popupUrl = [options.authorizationEndpoint, this.buildQueryString(response.data)].join('?'); if (this.$window['cordova']) { diff --git a/src/oauth2.ts b/src/oauth2.ts index ffd97c69..45ebc98c 100644 --- a/src/oauth2.ts +++ b/src/oauth2.ts @@ -39,6 +39,7 @@ export default class OAuth2 { constructor(private $http: angular.IHttpService, private $window: angular.IWindowService, private $timeout: angular.ITimeoutService, + private $q: angular.IQService, private SatellizerConfig: Config, private SatellizerPopup: Popup, private SatellizerStorage: Storage) { @@ -65,8 +66,8 @@ export default class OAuth2 { }; } - init(options: IOAuth2Options, userData: any): Promise { - return new Promise((resolve, reject) => { + init(options: IOAuth2Options, userData: any): angular.IPromise { + return this.$q((resolve, reject) => { angular.extend(this.defaults, options); this.$timeout(() => { @@ -82,7 +83,7 @@ export default class OAuth2 { this.SatellizerPopup.open(url, name, popupOptions); - this.SatellizerPopup.polling(redirectUri).then((oauth: any): void|Promise|angular.IHttpPromise => { + this.SatellizerPopup.polling(redirectUri).then((oauth: any): void|angular.IPromise|angular.IHttpPromise => { if (responseType === 'token' || !url) { return resolve(oauth); diff --git a/src/popup.ts b/src/popup.ts index 02afc3ce..94e32b09 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -3,8 +3,8 @@ import { parseQueryString, getFullUrlPath } from './utils'; export interface IPopup { open(url: string, name: string, popupOptions: { width: number, height: number }): void; stringifyOptions (options: any): string; - polling(redirectUri: string): Promise; - eventListener(redirectUri: string): Promise; + polling(redirectUri: string): angular.IPromise; + eventListener(redirectUri: string): angular.IPromise; } export default class Popup implements IPopup { @@ -15,7 +15,8 @@ export default class Popup implements IPopup { private defaults: { redirectUri: string }; constructor(private $interval: angular.IIntervalService, - private $window: angular.IWindowService) { + private $window: angular.IWindowService, + private $q: angular.IQService) { this.popup = null; this.url = 'about:blank'; // TODO remove this.defaults = { @@ -59,8 +60,8 @@ export default class Popup implements IPopup { // } } - polling(redirectUri: string): Promise { - return new Promise((resolve, reject) => { + polling(redirectUri: string): angular.IPromise { + return this.$q((resolve, reject) => { const redirectUriParser = document.createElement('a'); redirectUriParser.href = redirectUri; const redirectUriPath = getFullUrlPath(redirectUriParser); @@ -104,8 +105,8 @@ export default class Popup implements IPopup { }); } - eventListener(redirectUri): Promise { - return new Promise((resolve, reject) => { + eventListener(redirectUri): angular.IPromise { + return this.$q((resolve, reject) => { this.popup.addEventListener('loadstart', (event) => { if (!event.url.includes(redirectUri)) { return; diff --git a/test/authProvider.spec.ts b/test/authProvider.spec.ts index d8aac3b9..93a0a027 100644 --- a/test/authProvider.spec.ts +++ b/test/authProvider.spec.ts @@ -36,10 +36,10 @@ describe('AuthProvider', () => { httpBackend = $httpBackend; storage = new Storage($window, config); shared = new Shared($q, $window, $log, config, storage); - popup = new Popup($interval, $window); + popup = new Popup($interval, $window, $q); oauth1 = new OAuth1($http, $window, config, popup); - oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage); - oauth = new OAuth($http, config, shared, oauth1, oauth2); + oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage); + oauth = new OAuth($http, $q, config, shared, oauth1, oauth2); local = new Local($http, config, shared); auth = authProvider.$get(shared, local, oauth); })); diff --git a/test/oauth.spec.ts b/test/oauth.spec.ts index 3145b361..ca4003a0 100644 --- a/test/oauth.spec.ts +++ b/test/oauth.spec.ts @@ -22,10 +22,10 @@ describe('OAuth', () => { config = new Config(); storage = new Storage($window, config); shared = new Shared($q, $window, $log, config, storage); - popup = new Popup($interval, $window); + popup = new Popup($interval, $window, $q); oauth1 = new OAuth1($http, $window, config, popup); - oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage); - oauth = new OAuth($http, config, shared, oauth1, oauth2); + oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage); + oauth = new OAuth($http, $q, config, shared, oauth1, oauth2); })); afterEach(() => { diff --git a/test/oauth1.spec.ts b/test/oauth1.spec.ts index fb7cec0c..a7040848 100644 --- a/test/oauth1.spec.ts +++ b/test/oauth1.spec.ts @@ -18,7 +18,7 @@ describe('OAuth1', () => { config = new Config(); storage = new Storage($window, config); shared = new Shared($q, $window, $log, config, storage); - popup = new Popup($interval, $window); + popup = new Popup($interval, $window, $q); oauth1 = new OAuth1($http, $window, config, popup); })); diff --git a/test/oauth2.spec.ts b/test/oauth2.spec.ts index d0ea7e17..b1074028 100644 --- a/test/oauth2.spec.ts +++ b/test/oauth2.spec.ts @@ -26,8 +26,8 @@ describe('OAuth2', () => { config = new Config(); storage = new Storage($window, config); shared = new Shared($q, $window, $log, config, storage); - popup = new Popup($interval, $window); - oauth2 = new OAuth2($http, $window, $timeout, config, popup, storage); + popup = new Popup($interval, $window, $q); + oauth2 = new OAuth2($http, $window, $timeout, $q, config, popup, storage); })); afterEach(() => { diff --git a/test/popup.spec.ts b/test/popup.spec.ts index 3c7534a1..f4e31ecf 100644 --- a/test/popup.spec.ts +++ b/test/popup.spec.ts @@ -6,10 +6,10 @@ let popup; describe('Popup', () => { - beforeEach(angular.mock.inject(($interval, $window) => { + beforeEach(angular.mock.inject(($interval, $window, $q) => { interval = $interval; window = $window; - popup = new Popup($interval, $window); + popup = new Popup($interval, $window, $q); })); it('should be defined', () => {