Skip to content

🔥 [🐛?] Auth: listener passed to onIdTokenChanged() not being called on token refresh #8181

Open
@MartinCura

Description

@MartinCura

Issue

Hi all! The ID token is refreshed but my listener is never called, thus the app-wide Redux state is not updated with the new token and my app ends up using an expired Firebase token.

  const setUserToken = useCallback(
    (user: FirebaseAuthTypes.User | null) => {
      if (!user) {
        dispatch(authActions.clearToken());
        if (initializing) setInitializing(false);
        return;
      }
      user
        .getIdToken()
        .then((token) => dispatch(authActions.setToken(token)))
        .catch((error) => console.error(error));
    },
    [dispatch, initializing],
  );

  useEffect(() => {
    // No problem with login or logout
    const unsubscribe = auth().onAuthStateChanged((user) => {
      setAuthUser(user);
      setUserToken(user);
    });
    return unsubscribe;
  }, [setUserToken]);

  useEffect(() => {
    // *Why isn't this called on token refresh?* 👇
    const unsubscribe = auth().onIdTokenChanged(setUserToken);
    return unsubscribe;
  }, [setUserToken]);

  // On the other hand, this prints out the refreshed token 👇
  auth().currentUser?.getIdToken().then((token) => console.log(token));

I'm trying to understand if this is a setup issue somehow, but the rest seems to work correctly. I'm using Android but my colleagues are on iOS and having a similar problem. Issue goes away if user restarts the app, obviously. We are using Expo & EAS, if it's of any help.

Project Files

Javascript

Click To Expand

package.json:

{
    "@react-native-firebase/app": "^21.2.0",
    "@react-native-firebase/auth": "^21.2.0",
}

firebase.json for react-native-firebase v6:

# N/A

iOS

Click To Expand

ios/Podfile:

  • I'm not using Pods
  • I'm using Pods and my Podfile looks like:
# N/A

AppDelegate.m:

// N/A


Android

Click To Expand

Have you converted to AndroidX?

  • my application is an AndroidX application?
  • I am using android/gradle.settings jetifier=true for Android compatibility?
  • I am using the NPM package jetifier for react-native compatibility?

android/build.gradle:

// N/A

android/app/build.gradle:

// N/A

android/settings.gradle:

// N/A

MainApplication.java:

// N/A

AndroidManifest.xml:

<!-- N/A -->


Environment

Click To Expand

react-native info output:

% pnpm exec react-native info                                                                                                                                                   ✹ ✭
info Fetching system and libraries information...
System:
  OS: Linux 5.4 Ubuntu 20.04.6 LTS (Focal Fossa)
  CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Memory: 13.55 GB / 62.45 GB
  Shell:
    version: "5.8"
    path: /usr/bin/zsh
Binaries:
  Node:
    version: 20.16.0
    path: ~/.nvm/versions/node/v20.16.0/bin/node
  Yarn:
    version: 1.22.22
    path: ~/.yarn/bin/yarn
  npm:
    version: 10.8.1
    path: ~/.nvm/versions/node/v20.16.0/bin/npm
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
IDEs:
  Android Studio: AI-241.18034.62.2411.12169540
Languages:
  Java:
    version: 17.0.13
    path: /usr/bin/javac
  Ruby:
    version: 2.6.2
    path: /home/martin/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react: Not Found
  react-native: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

info React Native v0.76.3 is now available (your project is running on v0.74.5).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.76.3
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.74.5
info For more info, check out "https://reactnative.dev/docs/upgrading?os=linux".
  • Platform that you're experiencing the issue on:
    • iOS
    • Android
    • iOS but have not tested behavior on Android
    • Android but have not tested behavior on iOS
    • Both
  • react-native-firebase version you're using that has this issue:
    • 21.2.0
  • Firebase module(s) you're using that has the issue:
    • Auth
  • Are you using TypeScript?
    • Yes & 5.3.3

(Sorry if i'm missing some valuable info for debugging, haven't touched RN in some time so a lot of it is new for me; just ask and i'll provide!)


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