Skip to content
Advertisement

React Native – TypeError: _firebase.default.auth is not a function

I have a React Native App and have the following function:

import firebase from './firebase'

export function MainNavigator() {
  const [token, setToken] = useAtom(tokenAtom);

  useEffect(() => {
    firebase.auth().onIdTokenChanged(async (user) => {
      setToken(user ? await user.getIdToken() : null);
    });
  }, []);
  .....
  .... more code

This above code worked in Expo, but I have a new react native project (not built using expo) and this is failing:

I have the following in my package.json file:

"@react-native-firebase/app": "^13.0.1",
"@react-native-firebase/functions": "^13.0.1",
"firebase": "^9.5.0",

And my firebase.js file is like so (root directory)

import * as firebase from "firebase/app";

// Your web app's Firebase configuration
var firebaseConfig = {
    apiKey: "Secret stuff",
    authDomain: "Secret stuff",
    databaseURL: "Secret stuff",
    projectId: "Secret stuff",
    storageBucket: "Secret stuff",
    messagingSenderId: "Secret stuff",
    appId: "Secret stuff",
    measurementId: "Secret stuff"
};

// Initialize Firebase
firebase.initializeApp(firebaseConfig);

export default firebase;

Im getting the following error:

TypeError: _firebase.default.auth is not a function. (In ‘_firebase.default.auth()’, ‘_firebase.default.auth’ is undefined)

Advertisement

Answer

None of the code you show imports the Firebase Authentication SDK. So when you then try to use firebase.auth() it correctly indicates that it can’t find that SDK.

I’m guessing you want to import the Auth SDK too with something like:

import "firebase/auth";

I’d also recommend checking out the documentation on upgrading to the v9 or v9/compat SDK.

Advertisement