Skip to content

Auth does not persist if app is launched offline (React Native) #7164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thomas-coldwell opened this issue Mar 28, 2023 · 5 comments
Closed

Comments

@thomas-coldwell
Copy link

[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
@andrey3diq
Copy link

Same issue happens to me. I'm using ionic capacitor + angular. I tried different persistence storage options: browserSessionPersistence, indexedDBLocalPersistence, browserLocalPersistence - nothing worked

@andrey3diq
Copy link

I fixed it by using version "firebase": "9.8".

@pragatimodi
Copy link

@thomas-coldwell This comment suggests a solution to the same issue. Can you check if that fixes it?

@google-oss-bot
Copy link
Contributor

Hey @thomas-coldwell. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@google-oss-bot
Copy link
Contributor

Since there haven't been any recent updates here, I am going to close this issue.

@thomas-coldwell if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

@firebase firebase locked and limited conversation to collaborators Jul 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants