Firebase persistance breaks the app

Hi all,

When trying to add persistence to firebase Auth my app breaks. Perhaps anyone here knows what to do?

my error:

 ERROR  FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app).
 ERROR  Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.

My code:

import {
	FirebaseApp,
	FirebaseOptions,
	getApp,
	getApps,
	initializeApp,
} from 'firebase/app';

import {
	initializeAuth,
	getReactNativePersistence,
	Auth,
	getAuth,
} from 'firebase/auth/react-native';

import Constants from 'expo-constants';
import AsyncStorage from '@react-native-async-storage/async-storage';

const firebaseConfig: FirebaseOptions = {
	apiKey: Constants.manifest.extra.firebase.apiKey as string,
	authDomain: Constants.manifest.extra.firebase.authDomain as string,
	projectId: Constants.manifest.extra.firebase.projectId as string,
	storageBucket: Constants.manifest.extra.firebase.storageBucket as string,
	messagingSenderId: Constants.manifest.extra.firebase
		.messagingSenderId as string,
	appId: Constants.manifest.extra.firebase.appId as string,
	databaseURL: Constants.manifest.extra.firebase.databaseURL as string,
};

const app: FirebaseApp =
	getApps().length === 0 ? initializeApp(firebaseConfig) : getApp();

const auth: Auth =
	getApps().length === 0
		? initializeAuth(app, {
				persistence: getReactNativePersistence(AsyncStorage),
		  })
		: getAuth(app);



export {app, auth};

Hi @simpel

This “Invariant Violation” error can happen for a number of reasons. See here for some info:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.