Skip to content

Commit 817f6ec

Browse files
committed
fix: Add waitForRedirectDelay option to fix issues redirecting from Android when the browser is closed
1 parent 458cc71 commit 817f6ec

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ in case of vulnerabilities.
2222

2323
## [Unreleased]
2424

25+
### Added
26+
- Add `waitForRedirectDelay` option for **Android** to fix issues dismissing the browser before detecting the redirection with `Linking`.
27+
2528
## [3.0.1] - 2019-08-16
2629

2730
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Property | Description
152152
`animations` (Object) | Sets the start and exit animations. [`{ startEnter, startExit, endEnter, endExit }`]
153153
`headers` (Object) | The data are key/value pairs, they will be sent in the HTTP request headers for the provided url. [`{ 'Authorization': 'Bearer ...' }`]
154154
`forceCloseOnRedirection` (Boolean) | Open Custom Tab in a new task to avoid issues redirecting back to app scheme. [`true`/`false`]
155+
`waitForRedirectDelay` (Number) | Sets a delay for wait the redirection using `openAuth` method.
155156
156157
### Demo
157158

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ declare module 'react-native-inappbrowser-reborn' {
4949
endEnter: string,
5050
endExit: string
5151
},
52-
headers?: { [key: string]: string }
52+
headers?: { [key: string]: string },
53+
waitForRedirectDelay?: number
5354
}
5455

5556
export type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;

index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type InAppBrowserAndroidOptions = {
5555
endEnter: string,
5656
endExit: string
5757
},
58-
headers?: { [key: string]: string }
58+
headers?: { [key: string]: string },
59+
waitForRedirectDelay?: number
5960
};
6061

6162
type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;
@@ -66,25 +67,26 @@ async function open(
6667
): Promise<BrowserResult> {
6768
const modalEnabled =
6869
options.modalEnabled !== undefined ? options.modalEnabled : true;
69-
const inAppBrowseroptions = {
70+
const inAppBrowserOptions = {
7071
...options,
7172
url,
7273
dismissButtonStyle: options.dismissButtonStyle || 'close',
7374
readerMode: options.readerMode !== undefined ? options.readerMode : false,
7475
animated: options.animated !== undefined ? options.animated : true,
75-
modalEnabled
76+
modalEnabled,
77+
waitForRedirectDelay: options.waitForRedirectDelay || 0
7678
};
77-
if (inAppBrowseroptions.preferredBarTintColor) {
78-
inAppBrowseroptions.preferredBarTintColor = processColor(
79-
inAppBrowseroptions.preferredBarTintColor
79+
if (inAppBrowserOptions.preferredBarTintColor) {
80+
inAppBrowserOptions.preferredBarTintColor = processColor(
81+
inAppBrowserOptions.preferredBarTintColor
8082
);
8183
}
82-
if (inAppBrowseroptions.preferredControlTintColor) {
83-
inAppBrowseroptions.preferredControlTintColor = processColor(
84-
inAppBrowseroptions.preferredControlTintColor
84+
if (inAppBrowserOptions.preferredControlTintColor) {
85+
inAppBrowserOptions.preferredControlTintColor = processColor(
86+
inAppBrowserOptions.preferredControlTintColor
8587
);
8688
}
87-
return RNInAppBrowser.open(inAppBrowseroptions);
89+
return RNInAppBrowser.open(inAppBrowserOptions);
8890
}
8991

9092
function close(): void {
@@ -134,17 +136,23 @@ async function _openAuthSessionPolyfillAsync(
134136
!_redirectHandler,
135137
'InAppBrowser.openAuth is in a bad state. _redirectHandler is defined when it should not be.'
136138
);
137-
139+
let response = null;
138140
try {
139-
return await Promise.race([
140-
open(startUrl, options),
141+
response = await Promise.race([
142+
open(startUrl, options).then(result => {
143+
return new Promise(resolve => {
144+
// A delay to wait for the redirection or dismiss the browser instead
145+
setTimeout(() => resolve(result), options.waitForRedirectDelay);
146+
});
147+
}),
141148
_waitForRedirectAsync(returnUrl)
142149
]);
143150
} finally {
144151
close();
145152
Linking.removeEventListener('url', _redirectHandler);
146153
_redirectHandler = null;
147154
}
155+
return response;
148156
}
149157

150158
function _waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {

0 commit comments

Comments
 (0)