66 NativeModules ,
77 Platform ,
88 processColor ,
9- AppState
9+ AppState ,
10+ AppStateStatus
1011} from 'react-native' ;
1112
1213const { RNInAppBrowser } = NativeModules ;
@@ -157,7 +158,7 @@ async function _openAuthSessionPolyfillAsync(
157158function _waitForRedirectAsync ( returnUrl : string ) : Promise < RedirectResult > {
158159 return new Promise ( resolve => {
159160 _redirectHandler = ( event : RedirectEvent ) => {
160- if ( event . url . startsWith ( returnUrl ) ) {
161+ if ( event . url && event . url . startsWith ( returnUrl ) ) {
161162 resolve ( { url : event . url , type : 'success' } ) ;
162163 }
163164 } ;
@@ -166,35 +167,38 @@ function _waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
166167 } ) ;
167168}
168169
169- function _checkResultAndReturnUrl (
170+ /**
171+ * Detect Android Activity `OnResume` event once
172+ */
173+ function AppStateActiveOnce ( ) : Promise < void > {
174+ return new Promise ( function ( resolve ) {
175+ function _handleAppStateChange ( nextAppState : AppStateStatus ) {
176+ if ( nextAppState === 'active' ) {
177+ AppState . removeEventListener ( 'change' , _handleAppStateChange ) ;
178+ resolve ( ) ;
179+ }
180+ }
181+ AppState . addEventListener ( 'change' , _handleAppStateChange ) ;
182+ } ) ;
183+ }
184+
185+ async function _checkResultAndReturnUrl (
170186 returnUrl : string ,
171187 result : AuthSessionResult
172188) : Promise < AuthSessionResult > {
173- return new Promise ( function ( resolve ) {
174- if ( Platform . OS === 'android' && result . type !== 'cancel' ) {
175- /**
176- * Detect Android Activity OnResume event once
177- */
178- const _handleAppStateChange = async nextAppState => {
179- if ( nextAppState === 'active' ) {
180- try {
181- const url = await Linking . getInitialURL ( ) ;
182- if ( url && url . startsWith ( returnUrl ) ) {
183- resolve ( { url, type : 'success' } ) ;
184- } else {
185- resolve ( result ) ;
186- }
187- } catch ( error ) {
188- resolve ( result ) ;
189- }
190- AppState . removeEventListener ( 'change' , _handleAppStateChange ) ;
191- }
192- } ;
193- AppState . addEventListener ( 'change' , _handleAppStateChange ) ;
194- } else {
195- resolve ( result ) ;
189+ if ( Platform . OS === 'android' && result . type !== 'cancel' ) {
190+ try {
191+ await AppStateActiveOnce ( ) ;
192+ const url = await Linking . getInitialURL ( ) ;
193+ return url && url . startsWith ( returnUrl )
194+ ? { url, type : 'success' }
195+ : result ;
196+ } catch {
197+ return result ;
196198 }
197- } ) ;
199+ } else {
200+ return result ;
201+ }
198202}
199203
200204async function isAvailable ( ) : Promise < boolean > {
0 commit comments