Skip to content

Commit 309f7a9

Browse files
authored
Remove dependency on @react-native-async-storage/async-storage (#7570)
1 parent 78d2738 commit 309f7a9

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed

.changeset/eleven-lamps-give.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/auth': minor
3+
'firebase': minor
4+
---
5+
6+
Remove dependency on @react-native-async-storage/async-storage and add warnings to remind React Native users to manually import it.

packages/auth/index.rn.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323
*/
2424

2525
import { 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';
2929
import { registerAuth } from './src/core/auth/register';
3030
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';
3332

3433
// Core functionality shared by all clients
3534
export * from './index.shared';
@@ -49,7 +48,21 @@ export {
4948
// MFA
5049
export { 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

5467
export 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

6695
registerAuth(ClientPlatform.REACT_NATIVE);

packages/auth/package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,18 @@
103103
"typings:public": "node ../../scripts/build/use_typings.js ./dist/auth-public.d.ts"
104104
},
105105
"peerDependencies": {
106-
"@firebase/app": "0.x"
106+
"@firebase/app": "0.x",
107+
"@react-native-async-storage/async-storage": "^1.18.1"
108+
},
109+
"peerDependenciesMeta": {
110+
"@react-native-async-storage/async-storage": {
111+
"optional": true
112+
}
107113
},
108114
"dependencies": {
109115
"@firebase/component": "0.6.4",
110116
"@firebase/logger": "0.4.0",
111117
"@firebase/util": "1.9.3",
112-
"@react-native-async-storage/async-storage": "^1.18.1",
113118
"node-fetch": "2.6.7",
114119
"tslib": "^2.1.0"
115120
},

yarn.lock

-14
Original file line numberDiff line numberDiff line change
@@ -3263,13 +3263,6 @@
32633263
resolved "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz#a777360b5b39a1a2e5106f8e858f2fd2d060c570"
32643264
integrity sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=
32653265

3266-
"@react-native-async-storage/async-storage@^1.18.1":
3267-
version "1.19.0"
3268-
resolved "https://registry.npmjs.org/@react-native-async-storage/async-storage/-/async-storage-1.19.0.tgz#594aca9c20924b7955d62cf43797b4187e1e6cf8"
3269-
integrity sha512-xOFkz/FaQctD6yNJDur+WnHdSTigOs3pTz6HmfC8X8PYwcnnN3R9UxuWiwsfK8vvT2WioAxUkQt3lB7GySNA2w==
3270-
dependencies:
3271-
merge-options "^3.0.4"
3272-
32733266
32743267
version "3.1.9"
32753268
resolved "https://registry.npmjs.org/@rollup/plugin-alias/-/plugin-alias-3.1.9.tgz#a5d267548fe48441f34be8323fb64d1d4a1b3fdf"
@@ -12290,13 +12283,6 @@ [email protected]:
1229012283
resolved "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61"
1229112284
integrity sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=
1229212285

12293-
merge-options@^3.0.4:
12294-
version "3.0.4"
12295-
resolved "https://registry.npmjs.org/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
12296-
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
12297-
dependencies:
12298-
is-plain-obj "^2.1.0"
12299-
1230012286
merge-stream@^2.0.0:
1230112287
version "2.0.0"
1230212288
resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"

0 commit comments

Comments
 (0)