Skip to content

Commit bb83505

Browse files
hrangelHenrique Rangel
authored andcommitted
Support IONIC InAppBrowser - no dependenciesCI error-warnings fixes
1 parent e08fd40 commit bb83505

File tree

3 files changed

+81
-33
lines changed

3 files changed

+81
-33
lines changed

package-lock.json

Lines changed: 11 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

projects/angular-token/src/lib/angular-token.model.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ export interface UserType {
5959
path: string;
6060
}
6161

62+
export interface TokenInAppBrowser<T extends {}, Y extends {}> {
63+
create(url: string, target?: string, options?: string | Y): T;
64+
}
65+
66+
export interface TokenPlatform {
67+
is(platformName: string): boolean;
68+
}
69+
6270
export interface AngularTokenOptions {
6371
angularTokenOptionsProvider?: Provider;
6472

@@ -90,4 +98,5 @@ export interface AngularTokenOptions {
9098
oAuthCallbackPath?: string;
9199
oAuthWindowType?: string;
92100
oAuthWindowOptions?: { [key: string]: string; };
101+
oAuthBrowserCallbacks?: { [key: string]: string; };
93102
}

projects/angular-token/src/lib/angular-token.service.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)