[REQUIRED] Describe your environment
- Operating System version: M1 MacBook Pro, MacOS (Monterey 12.6)
- Browser version: Chrome 111.0.5563.110 (Official Build) (arm64)
- Firebase SDK version: 9.18.0
- Firebase Product: Auth
[REQUIRED] Describe the problem
I have been developing a React Native app that uses the Firebase JS SDK for Auth. It has a simple email + password options setup and I had set this to use persistence as follows:
import AsyncStorage from "@react-native-async-storage/async-storage";
import { initializeApp } from "firebase/app";
import {
getReactNativePersistence,
initializeAuth,
} from "firebase/auth/react-native";
export const firebaseApp = initializeApp({
/* ... */
});
export const auth = initializeAuth(firebaseApp, {
persistence: getReactNativePersistence(AsyncStorage),
});
This works well while I remain fully online and auth is properly persisted between app launches and sessions. However, if I close the app, turn on airplane mode (so no connectivity) and then relaunch the app the auth is no longer persisted and is actually cleared from the async storage entirely.
It is my assumption that in this case the expected behaviour would be that the auth is persisted even if the app is offline (e.g. auth.currentUser != null)
I have managed to track down the reason for this not persisting the auth and clearing it from async storage:
- In
initializeCurrentUser it gets the persisted user (which it does have when it launches offline) and then it calls reloadAndSetCurrentUserOrClear
- This then calls
_reloadWithoutSaving and then makes a request to get the account info.
- As we are offline this fails resulting in an
auth/internal-error and then in reloadAndSetCurrentUserOrClear this throws causing the user to be immediately cleared and wiped from async storage
Again I'm not too sure what the expected behaviour is in this case where it hits an auth/internal-error while initialising. Should it use the persisted auth or clear it? I noticed here you only clear it if the error is not a network request error so maybe it also needs to check the error is not an auth/internal-error here as well to prevent it clearing?
Steps to reproduce:
- Use the above code to initialise auth in a React Native app
- Sign in with an email and password
- Close the app fully
- Put the device into airplane mode
- Reopen the app and see that the
auth.currentUser is reported as null
Relevant Code:
- See above code snippet for persistent auth
[REQUIRED] Describe your environment
[REQUIRED] Describe the problem
I have been developing a React Native app that uses the Firebase JS SDK for Auth. It has a simple email + password options setup and I had set this to use persistence as follows:
This works well while I remain fully online and auth is properly persisted between app launches and sessions. However, if I close the app, turn on airplane mode (so no connectivity) and then relaunch the app the auth is no longer persisted and is actually cleared from the async storage entirely.
It is my assumption that in this case the expected behaviour would be that the auth is persisted even if the app is offline (e.g.
auth.currentUser != null)I have managed to track down the reason for this not persisting the auth and clearing it from async storage:
initializeCurrentUserit gets the persisted user (which it does have when it launches offline) and then it callsreloadAndSetCurrentUserOrClear_reloadWithoutSavingand then makes a request to get the account info.auth/internal-errorand then inreloadAndSetCurrentUserOrClearthis throws causing the user to be immediately cleared and wiped from async storageAgain I'm not too sure what the expected behaviour is in this case where it hits an
auth/internal-errorwhile initialising. Should it use the persisted auth or clear it? I noticed here you only clear it if the error is not a network request error so maybe it also needs to check the error is not anauth/internal-errorhere as well to prevent it clearing?Steps to reproduce:
auth.currentUseris reported asnullRelevant Code: