6
6
NativeModules ,
7
7
Platform ,
8
8
processColor ,
9
- AppState
9
+ AppState ,
10
+ AppStateStatus
10
11
} from 'react-native' ;
11
12
12
13
const { RNInAppBrowser } = NativeModules ;
@@ -157,7 +158,7 @@ async function _openAuthSessionPolyfillAsync(
157
158
function _waitForRedirectAsync ( returnUrl : string ) : Promise < RedirectResult > {
158
159
return new Promise ( resolve => {
159
160
_redirectHandler = ( event : RedirectEvent ) => {
160
- if ( event . url . startsWith ( returnUrl ) ) {
161
+ if ( event . url && event . url . startsWith ( returnUrl ) ) {
161
162
resolve ( { url : event . url , type : 'success' } ) ;
162
163
}
163
164
} ;
@@ -166,35 +167,38 @@ function _waitForRedirectAsync(returnUrl: string): Promise<RedirectResult> {
166
167
} ) ;
167
168
}
168
169
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 (
170
186
returnUrl : string ,
171
187
result : AuthSessionResult
172
188
) : 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 ;
196
198
}
197
- } ) ;
199
+ } else {
200
+ return result ;
201
+ }
198
202
}
199
203
200
204
async function isAvailable ( ) : Promise < boolean > {
0 commit comments