Skip to content

Commit

Permalink
fix: Add waitForRedirectDelay option to fix issues redirecting from A…
Browse files Browse the repository at this point in the history
…ndroid when the browser is closed
  • Loading branch information
jdnichollsc committed Sep 3, 2019
1 parent 458cc71 commit 817f6ec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ in case of vulnerabilities.

## [Unreleased]

### Added
- Add `waitForRedirectDelay` option for **Android** to fix issues dismissing the browser before detecting the redirection with `Linking`.

## [3.0.1] - 2019-08-16

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

### Demo

Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ declare module 'react-native-inappbrowser-reborn' {
endEnter: string,
endExit: string
},
headers?: { [key: string]: string }
headers?: { [key: string]: string },
waitForRedirectDelay?: number
}

export type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;
Expand Down
34 changes: 21 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type InAppBrowserAndroidOptions = {
endEnter: string,
endExit: string
},
headers?: { [key: string]: string }
headers?: { [key: string]: string },
waitForRedirectDelay?: number
};

type InAppBrowserOptions = InAppBrowserAndroidOptions | InAppBrowseriOSOptions;
Expand All @@ -66,25 +67,26 @@ async function open(
): Promise<BrowserResult> {
const modalEnabled =
options.modalEnabled !== undefined ? options.modalEnabled : true;
const inAppBrowseroptions = {
const inAppBrowserOptions = {
...options,
url,
dismissButtonStyle: options.dismissButtonStyle || 'close',
readerMode: options.readerMode !== undefined ? options.readerMode : false,
animated: options.animated !== undefined ? options.animated : true,
modalEnabled
modalEnabled,
waitForRedirectDelay: options.waitForRedirectDelay || 0
};
if (inAppBrowseroptions.preferredBarTintColor) {
inAppBrowseroptions.preferredBarTintColor = processColor(
inAppBrowseroptions.preferredBarTintColor
if (inAppBrowserOptions.preferredBarTintColor) {
inAppBrowserOptions.preferredBarTintColor = processColor(
inAppBrowserOptions.preferredBarTintColor
);
}
if (inAppBrowseroptions.preferredControlTintColor) {
inAppBrowseroptions.preferredControlTintColor = processColor(
inAppBrowseroptions.preferredControlTintColor
if (inAppBrowserOptions.preferredControlTintColor) {
inAppBrowserOptions.preferredControlTintColor = processColor(
inAppBrowserOptions.preferredControlTintColor
);
}
return RNInAppBrowser.open(inAppBrowseroptions);
return RNInAppBrowser.open(inAppBrowserOptions);
}

function close(): void {
Expand Down Expand Up @@ -134,17 +136,23 @@ async function _openAuthSessionPolyfillAsync(
!_redirectHandler,
'InAppBrowser.openAuth is in a bad state. _redirectHandler is defined when it should not be.'
);

let response = null;
try {
return await Promise.race([
open(startUrl, options),
response = await Promise.race([
open(startUrl, options).then(result => {
return new Promise(resolve => {
// A delay to wait for the redirection or dismiss the browser instead
setTimeout(() => resolve(result), options.waitForRedirectDelay);
});
}),
_waitForRedirectAsync(returnUrl)
]);
} finally {
close();
Linking.removeEventListener('url', _redirectHandler);
_redirectHandler = null;
}
return response;
}

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

0 comments on commit 817f6ec

Please sign in to comment.