Skip to content

Commit 2060ec7

Browse files
authored
Merge pull request #100 from proyecto26/85-response-type-cancel-on-device-during-live-sync
Fix auth redirection logic
2 parents 74a039c + 3cb716c commit 2060ec7

File tree

7 files changed

+41
-34
lines changed

7 files changed

+41
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ in case of vulnerabilities.
3131
- Complete migration to ns8 and fix build issues by [@rigor789](https://github.com/rigor789) ([#92](https://github.com/proyecto26/nativescript-inappbrowser/pull/92)).
3232
- Fix support for `Metadata Filtering` from Android by [@jcassidyav](https://github.com/jcassidyav) ([#93](https://github.com/proyecto26/nativescript-inappbrowser/pull/93)).
3333
- Avoid stringifying null redirect url by [@rmartin48](https://github.com/rmartin48) ([#99](https://github.com/proyecto26/nativescript-inappbrowser/pull/99)).
34+
- Fix auth redirection logic by [@jdnichollsc](https://github.com/jdnichollsc) ([#100](https://github.com/proyecto26/nativescript-inappbrowser/pull/100)).
3435

3536
### Removed
3637
- Remove `QUERY_ALL_PACKAGES` permission by [@edusperoni](https://github.com/edusperoni) ([#87](https://github.com/proyecto26/nativescript-inappbrowser/pull/87)).

src/ChromeTabsManagerActivity.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Context = android.content.Context;
22
import Intent = android.content.Intent;
33
import Bundle = android.os.Bundle;
4+
import Log = android.util.Log;
45

56
import { Observable } from "@nativescript/core";
67
import { BROWSER_TYPES } from "./InAppBrowser.common";
78
import { DISMISSED_EVENT } from "./utils.android";
8-
import { log } from "./utils.common";
99

1010
class ChromeTabsEvent extends Observable {
1111
public message: string;
@@ -18,6 +18,7 @@ const BROWSER_ACTIVITY_EVENTS = new ChromeTabsEvent();
1818
const KEY_BROWSER_INTENT = "browserIntent";
1919
const BROWSER_RESULT_TYPE = "browserResultType";
2020
const DEFAULT_RESULT_TYPE = BROWSER_TYPES.DISMISS;
21+
const TAG = "ChromeTabsManagerActivity";
2122

2223
const notifyMessage = (
2324
message: string,
@@ -74,7 +75,7 @@ class ChromeTabsManagerActivity extends android.app.Activity {
7475
this.isError = true;
7576
notifyMessage("Unable to open url.", this.resultType, this.isError);
7677
this.finish();
77-
log(`InAppBrowser: ${error}`);
78+
Log.e(TAG, error.message);
7879
}
7980
}
8081

src/InAppBrowser.android.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,15 @@ function setup() {
301301
redirectUrl: string,
302302
options?: InAppBrowserOptions
303303
) {
304-
let response = null;
305304
try {
306-
response = await openAuthSessionPolyfillAsync(
307-
(startUrl, opt) => this.open(startUrl, opt),
308-
url,
309-
redirectUrl,
310-
options
305+
return await openAuthSessionPolyfillAsync(
306+
() => this.open(url, options),
307+
redirectUrl
311308
);
312309
} finally {
313310
closeAuthSessionPolyfillAsync();
314311
this.close();
315312
}
316-
return response;
317313
}
318314

319315
public closeAuth(): void {

src/InAppBrowser.ios.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
setModalInPresentation,
2020
dismissWithoutAnimation,
2121
InAppBrowserOpenAuthErrorMessage,
22+
getWindow,
2223
} from "./utils.ios";
2324

2425
const DEFAULT_PROTOCOLS = [
@@ -142,8 +143,8 @@ function setup() {
142143
}
143144
}
144145

145-
const ctrl =
146-
UIApplication.sharedApplication.keyWindow.rootViewController;
146+
const window = getWindow();
147+
const ctrl = window.rootViewController;
147148
if (modalEnabled) {
148149
// This is a hack to present the SafariViewController modally
149150
const safariHackVC =
@@ -198,7 +199,8 @@ function setup() {
198199
});
199200
}
200201
public close() {
201-
const ctrl = UIApplication.sharedApplication.keyWindow.rootViewController;
202+
const window = getWindow();
203+
const ctrl = window.rootViewController;
202204
ctrl.dismissViewControllerAnimatedCompletion(this.animated, () => {
203205
if (this.redirectResolve) {
204206
this.redirectResolve({
@@ -286,7 +288,7 @@ function setup() {
286288
public presentationAnchorForWebAuthenticationSession(
287289
_: ASWebAuthenticationSession
288290
): UIWindow {
289-
return UIApplication.sharedApplication.keyWindow;
291+
return getWindow();
290292
}
291293
public safariViewControllerDidFinish(
292294
controller: SFSafariViewController

src/utils.android.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import Intent = android.content.Intent;
33
import NfcAdapter = android.nfc.NfcAdapter;
44
import Context = android.content.Context;
55
import ResolveInfo = android.content.pm.ResolveInfo;
6-
import Color = android.graphics.Color;
76
import List = java.util.List;
87
import Arrays = java.util.Arrays;
98

@@ -16,9 +15,8 @@ import {
1615
} from "@nativescript/core";
1716
import {
1817
AuthSessionResult,
18+
BrowserResult,
1919
BROWSER_TYPES,
20-
InAppBrowserOptions,
21-
OpenBrowserAsync,
2220
RedirectResult,
2321
} from "./InAppBrowser.common";
2422

@@ -61,6 +59,7 @@ export const DISMISSED_EVENT = "DismissedEvent";
6159
* Save the handler of the redirection event in order to removes listener later.
6260
*/
6361
let _redirectHandler: (args: ApplicationEventData) => void;
62+
6463
/**
6564
* Save the previous url in order to avoid loading the same data for a new Authentication flow.
6665
*/
@@ -109,17 +108,15 @@ function waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
109108
function handleAppStateActiveOnce(): Promise<Activity> {
110109
return new Promise(function (resolve) {
111110
// Browser can be closed before handling AppState change
112-
if (!Application.android.paused) {
113-
const activity =
114-
Application.android.foregroundActivity ||
115-
Application.android.startActivity;
116-
return resolve(activity);
111+
if (!Application.android.paused && Application.android.foregroundActivity) {
112+
resolve(Application.android.foregroundActivity);
113+
}
114+
function handleAppStateChange(args: AndroidActivityEventData) {
115+
resolve(args.activity);
117116
}
118117
Application.android.once(
119118
AndroidApplication.activityResumedEvent,
120-
function (args: AndroidActivityEventData) {
121-
resolve(args.activity);
122-
}
119+
handleAppStateChange,
123120
);
124121
});
125122
}
@@ -143,18 +140,16 @@ async function checkResultAndReturnUrl(
143140
}
144141

145142
/* Android polyfill for AuthenticationSession flow */
146-
export function openAuthSessionPolyfillAsync(
147-
open: OpenBrowserAsync,
148-
startUrl: string,
143+
export async function openAuthSessionPolyfillAsync(
144+
open: () => Promise<BrowserResult>,
149145
returnUrl: string,
150-
options?: InAppBrowserOptions
151146
): Promise<AuthSessionResult> {
152-
return Promise.race([
153-
waitForRedirectAsync(returnUrl),
154-
open(startUrl, options).then(function (result) {
147+
return await Promise.race([
148+
open().then(function (result) {
155149
return checkResultAndReturnUrl(returnUrl, result);
156150
}),
157-
]);
151+
waitForRedirectAsync(returnUrl),
152+
])
158153
}
159154

160155
export function closeAuthSessionPolyfillAsync(): void {

src/utils.common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Color } from "@nativescript/core";
1+
import { Application, Color } from "@nativescript/core";
22

33
export function parseColor(color: string | Color) {
44
if (color && !(color instanceof Color)) {
@@ -23,5 +23,8 @@ export function log(message: string, ...optionalParams: any[]): void {
2323
if (nglog) {
2424
nglog(message, ...optionalParams);
2525
}
26+
if (Application.android) {
27+
android.util.Log.d("JS", message);
28+
}
2629
console.log(message, ...optionalParams);
2730
}

src/utils.ios.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export function getTransitionStyle(styleKey: string): UIModalTransitionStyle {
4040
: UIModalTransitionStyle.CoverVertical;
4141
}
4242

43+
export function getWindow(): UIWindow {
44+
const sharedApplication = UIApplication.sharedApplication;
45+
if (sharedApplication.windows.count > 0 && sharedApplication.windows[0]) {
46+
return sharedApplication.windows[0];
47+
}
48+
return sharedApplication.keyWindow;
49+
}
50+
4351
export function dismissWithoutAnimation(
4452
controller: SFSafariViewController
4553
): void {
@@ -54,7 +62,8 @@ export function dismissWithoutAnimation(
5462
controller.view.alpha = 0.05;
5563
controller.view.frame = CGRectMake(0.0, 0.0, 0.5, 0.5);
5664

57-
const ctrl = UIApplication.sharedApplication.keyWindow.rootViewController;
65+
const window = getWindow();
66+
const ctrl = window.rootViewController;
5867
ctrl.view.layer.addAnimationForKey(transition, animationKey);
5968
ctrl.dismissViewControllerAnimatedCompletion(false, () => {
6069
ctrl.view.layer.removeAnimationForKey(animationKey);

0 commit comments

Comments
 (0)