2323 */
2424
2525import { FirebaseApp , getApp , _getProvider } from '@firebase/app' ;
26- import { Auth } from './src/model/public_types' ;
26+ import { Auth , Dependencies } from './src/model/public_types' ;
2727
28- import { initializeAuth } from './src' ;
28+ import { initializeAuth as initializeAuthOriginal } from './src' ;
2929import { registerAuth } from './src/core/auth/register' ;
3030import { 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' ;
3332
3433// Core functionality shared by all clients
3534export * from './index.shared' ;
@@ -49,7 +48,21 @@ export {
4948// MFA
5049export { PhoneMultiFactorGenerator } from './src/platform_browser/mfa/assertions/phone' ;
5150
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+ ` ;
5366
5467export function getAuth ( app : FirebaseApp = getApp ( ) ) : Auth {
5568 const provider = _getProvider ( app , 'auth' ) ;
@@ -58,9 +71,25 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
5871 return provider . getImmediate ( ) ;
5972 }
6073
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 ) ;
6493}
6594
6695registerAuth ( ClientPlatform . REACT_NATIVE ) ;
0 commit comments