Skip to content
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

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

Open
2 of 10 tasks
MartinCura opened this issue Dec 5, 2024 · 1 comment
Open
2 of 10 tasks
Labels

Comments

@MartinCura
Copy link

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!)


@mikehardy
Copy link
Collaborator

I encourage you to update to current versions and make sure it is still there. There was a related problem fixed on android but it was failure to call items after hot reload. It would not have affected iOS and you don't mention hot reload, but who knows - logging on old versions is frequently unproductive as things may have been fixed after

If you can still reproduce, I'd love to hear what we need to do for a minimal test case to show the problem

Starting from npx react-native init this creates a fresh react-native-firebase project, you can control the package names (as can I) so they match a set of android/ios google-services config files for firebase https://github.com/mikehardy/rnfbdemo/blob/main/make-demo.sh

But otherwise the app is just one page. You can hack in buttons as I did to test basic things and it would be great to know what we can put in there to trigger this quickly and confirm there's a repo problem vs a project-specific problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants