@@ -18,7 +18,10 @@ import {
1818 UserData ,
1919 AuthData ,
2020
21- AngularTokenOptions
21+ AngularTokenOptions ,
22+
23+ TokenPlatform ,
24+ TokenInAppBrowser ,
2225} from './angular-token.model' ;
2326
2427@Injectable ( {
@@ -120,6 +123,10 @@ export class AngularTokenService implements CanActivate {
120123 oAuthCallbackPath : 'oauth_callback' ,
121124 oAuthWindowType : 'newWindow' ,
122125 oAuthWindowOptions : null ,
126+
127+ oAuthBrowserCallbacks : {
128+ github : 'auth/github/callback' ,
129+ } ,
123130 } ;
124131
125132 const mergedOptions = ( < any > Object ) . assign ( defaultOptions , config ) ;
@@ -215,14 +222,15 @@ export class AngularTokenService implements CanActivate {
215222 return observ ;
216223 }
217224
218- signInOAuth ( oAuthType : string ) {
225+ signInOAuth ( oAuthType : string , inAppBrowser ?: TokenInAppBrowser < any , any > , platform ?: TokenPlatform ) {
219226
220227 const oAuthPath : string = this . getOAuthPath ( oAuthType ) ;
221228 const callbackUrl = `${ this . global . location . origin } /${ this . options . oAuthCallbackPath } ` ;
222229 const oAuthWindowType : string = this . options . oAuthWindowType ;
223230 const authUrl : string = this . getOAuthUrl ( oAuthPath , callbackUrl , oAuthWindowType ) ;
224231
225- if ( oAuthWindowType === 'newWindow' ) {
232+ if ( oAuthWindowType === 'newWindow' ||
233+ ( oAuthWindowType == 'inAppBrowser' && ( ! platform || ! platform . is ( 'cordova' ) || ! ( platform . is ( 'ios' ) || platform . is ( 'android' ) ) ) ) ) {
226234 const oAuthWindowOptions = this . options . oAuthWindowOptions ;
227235 let windowOptions = '' ;
228236
@@ -240,6 +248,56 @@ export class AngularTokenService implements CanActivate {
240248 `closebuttoncaption=Cancel${ windowOptions } `
241249 ) ;
242250 return this . requestCredentialsViaPostMessage ( popup ) ;
251+ } else if ( oAuthWindowType == 'inAppBrowser' ) {
252+ let oAuthBrowserCallback = this . options . oAuthBrowserCallbacks [ oAuthType ] ;
253+ if ( ! oAuthBrowserCallback ) {
254+ throw new Error ( `To login with oAuth provider ${ oAuthType } using inAppBrowser the callback (in oAuthBrowserCallbacks) is required.` ) ;
255+ }
256+ // let oAuthWindowOptions = this.options.oAuthWindowOptions;
257+ // let windowOptions = '';
258+
259+ // if (oAuthWindowOptions) {
260+ // for (let key in oAuthWindowOptions) {
261+ // windowOptions += `,${key}=${oAuthWindowOptions[key]}`;
262+ // }
263+ // }
264+
265+ let browser = inAppBrowser . create (
266+ authUrl ,
267+ '_blank' ,
268+ 'location=no'
269+ ) ;
270+
271+ return new Observable ( ( observer ) => {
272+ browser . on ( 'loadstop' ) . subscribe ( ( ev : any ) => {
273+ if ( ev . url . indexOf ( oAuthBrowserCallback ) > - 1 ) {
274+ browser . executeScript ( { code : "requestCredentials();" } ) . then ( ( credentials : any ) => {
275+ this . getAuthDataFromPostMessage ( credentials [ 0 ] ) ;
276+
277+ let pollerObserv = interval ( 400 ) ;
278+
279+ let pollerSubscription = pollerObserv . subscribe ( ( ) => {
280+ if ( this . userSignedIn ( ) ) {
281+ observer . next ( this . authData ) ;
282+ observer . complete ( ) ;
283+
284+ pollerSubscription . unsubscribe ( ) ;
285+ browser . close ( ) ;
286+ }
287+ } , ( error : any ) => {
288+ observer . error ( error ) ;
289+ observer . complete ( ) ;
290+ } ) ;
291+ } , ( error : any ) => {
292+ observer . error ( error ) ;
293+ observer . complete ( ) ;
294+ } ) ;
295+ }
296+ } , ( error : any ) => {
297+ observer . error ( error ) ;
298+ observer . complete ( ) ;
299+ } ) ;
300+ } )
243301 } else if ( oAuthWindowType === 'sameWindow' ) {
244302 this . global . location . href = authUrl ;
245303 } else {
0 commit comments