23
23
*/
24
24
25
25
import { FirebaseApp , getApp , _getProvider } from '@firebase/app' ;
26
- import { Auth } from './src/model/public_types' ;
26
+ import { Auth , Dependencies } from './src/model/public_types' ;
27
27
28
- import { initializeAuth } from './src' ;
28
+ import { initializeAuth as initializeAuthOriginal } from './src' ;
29
29
import { registerAuth } from './src/core/auth/register' ;
30
30
import { ClientPlatform } from './src/core/util/version' ;
31
- import { getReactNativePersistence } from './src/platform_react_native/persistence/react_native' ;
32
- import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage' ;
31
+ import { _logWarn } from './src/core/util/log' ;
33
32
34
33
// Core functionality shared by all clients
35
34
export * from './index.shared' ;
@@ -49,7 +48,21 @@ export {
49
48
// MFA
50
49
export { PhoneMultiFactorGenerator } from './src/platform_browser/mfa/assertions/phone' ;
51
50
52
- export { getReactNativePersistence } ;
51
+ export { getReactNativePersistence } from './src/platform_react_native/persistence/react_native' ;
52
+
53
+ const NO_PERSISTENCE_WARNING = `
54
+ You are initializing Firebase Auth for React Native without providing
55
+ AsyncStorage. Auth state will default to memory persistence and will not
56
+ persist between sessions. In order to persist auth state, install the package
57
+ "@react-native-async-storage/async-storage" and provide it to
58
+ initializeAuth:
59
+
60
+ import { initializeAuth, getReactNativePersistence } from 'firebase/auth';
61
+ import ReactNativeAsyncStorage from '@react-native-async-storage/async-storage';
62
+ const auth = initializeAuth(app, {
63
+ persistence: getReactNativePersistence(ReactNativeAsyncStorage)
64
+ });
65
+ ` ;
53
66
54
67
export function getAuth ( app : FirebaseApp = getApp ( ) ) : Auth {
55
68
const provider = _getProvider ( app , 'auth' ) ;
@@ -58,9 +71,25 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
58
71
return provider . getImmediate ( ) ;
59
72
}
60
73
61
- return initializeAuth ( app , {
62
- persistence : getReactNativePersistence ( ReactNativeAsyncStorage )
63
- } ) ;
74
+ // Only warn if getAuth() is called before initializeAuth()
75
+ _logWarn ( NO_PERSISTENCE_WARNING ) ;
76
+
77
+ return initializeAuthOriginal ( app ) ;
78
+ }
79
+
80
+ /**
81
+ * Wrapper around base `initializeAuth()` for RN users only, which
82
+ * shows the warning message if no persistence is provided.
83
+ * Double-checked potential collision with `export * from './index.shared'`
84
+ * as `./index.shared` also exports `initializeAuth()`, and the final
85
+ * bundle does correctly export only this `initializeAuth()` function
86
+ * and not the one from index.shared.
87
+ */
88
+ export function initializeAuth ( app : FirebaseApp , deps ?: Dependencies ) : Auth {
89
+ if ( ! deps ?. persistence ) {
90
+ _logWarn ( NO_PERSISTENCE_WARNING ) ;
91
+ }
92
+ return initializeAuthOriginal ( app , deps ) ;
64
93
}
65
94
66
95
registerAuth ( ClientPlatform . REACT_NATIVE ) ;
0 commit comments