Skip to content

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

Description

@thomas-coldwell

[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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions