How to initialize firebase in firebase version 9 (firebase v9)? - javascript

I used to initialize my firebase app using this method -
import firebase from "firebase/app";
import "firebase/auth";
const config = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
export default function initFirebase() {
if (!firebase.apps.length) {
firebase.initializeApp(config);
}
}
I am upgrading my firebase version to 9 and I need a way to initialize my app using es modules.
I tried this -
import { initializeApp, getApps, getApp } from "firebase/app";
const config = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
export default function initFirebase() {
getApps().length === 0 ? initializeApp(config) : getApp();
}
But I am getting this error -
FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app-compat/no-app).

Why don't you simply use initializeApp?
import { initializeApp } from "firebase/app";
const config = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.NEXT_PUBLIC_FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
export const app = initializeApp(config);
Do you really need your custom initFirebase() function?

Related

NextAuth and Firebase causing SyntaxError Cannot use import statement outside a module

I'm new to both Next.js and Firebase. I've been trying to use NextAuth.js to authenticate with Discord for my firebase app. However, I ran into an error.
I've tried adding "type": "module" into my package.json file and converting all my imports into requires instead but I still get the same error.
This is my firebase.js
import firebase from "firebase/compat/app";
import { getApps, getApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getStorage } from 'firebase/storage';
export const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID
};
export const app = getApps.length > 0 ? getApp() : firebase.initializeApp(firebaseConfig);
export const firestore = getFirestore(app);
export const storage = getStorage(app);
This is my [...nextauth].js
import NextAuth from "next-auth";
import DiscordProvider from 'next-auth/providers/discord';
import { FirestoreAdapter } from "#next-auth/firebase-adapter";
import { firebaseConfig } from "../../../lib/firebase";
export default NextAuth({
providers: [
DiscordProvider({
clientId: process.env.DISCORD_ID,
clientSecret: process.env.DISCORD_SECRET
}),
],
adapter: FirestoreAdapter(firebaseConfig),
})
Any help would be much appreciated
I went through this as well. I found using a simple config easier and exporting the default app into your component. Then in the component, add the extra functionality.
Here is my config;
import { initializeApp } from 'firebase/app';
// Initialize Firebase
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
databaseURL: process.env.FIREBASE_DATABASE_URL,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
}
const app = initializeApp(firebaseConfig)
export default app;
then in controller...
import { getDatabase } from 'firebase/database';
import app from '../services/firebase';
const rtdb = getDatabase(app);

React Firebase Integration - TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0__.default is undefined

Stack Overflow! Long time no see.
I'm developing a web app with react where you can see definitions that I wrote in a firebase database for modern concepts, like typing quirks and plurality. When trying to implement my firebase firestore database in my app, it fails with the following error:
TypeError: firebase_app__WEBPACK_IMPORTED_MODULE_0__.default is undefined.
Here is my code in firebase.js:
import firebase from "firebase/compat/app";
import "firebase/compat/firestore";
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
apiKey: "dont-steal-my-db",
authDomain: "please-respond",
databaseURL: "https://definition-2d49c-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "definition-2d49c",
storageBucket: "definition-2d49c.appspot.com",
messagingSenderId: "404265179808",
appId: "1:404265179808:web:10f7b7f917d695dabb87c5",
measurementId: "G-SYN64MDXL1"
};
firebase.initializeApp(firebaseConfig);
export default firebase;
How can I fix this error, and where is a good place to go in the future for problems with firebase like these?
//try this :
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {
apiKey: "dont-steal-my-db",
authDomain: "please-respond",
databaseURL: "https://definition-2d49c-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "definition-2d49c",
storageBucket: "definition-2d49c.appspot.com",
messagingSenderId: "404265179808",
appId: "1:404265179808:web:10f7b7f917d695dabb87c5",
measurementId: "G-SYN64MDXL1"
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
export const db = firebaseApp.firestore();

Attempted import error: 'messaging' is not exported from 'firebase/app'

import firebase from "firebase/app";
import "firebase/messaging";
let messaging = null
if (firebase.messaging.isSupported()) {
const initializedFirebaseApp = firebase.initializeApp({
// Project Settings => Add Firebase to your web app
apiKey: process.env.REACT_APP_FIREBASE_APIKEY,
authDomain: process.env.REACT_APP_FIREBASE_AUTHDOMAIN,
databaseURL: process.env.REACT_APP_FIREBASE_DBURL,
projectId: process.env.REACT_APP_FIREBASE_PROJECTID,
storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.REACT_APP_FIREBASE_MSG_SENDERID,
appId: process.env.REACT_APP_FIREBASE_APPID
});
messaging = initializedFirebaseApp.messaging();
}
export { messaging };
I am starting to run my server am getting failed to compile errors below Like this:
Attempted import error: 'messaging' is not exported from 'firebase/app' (imported as 'firebase').
How can i fix this ???
enter image description here

React and Firebase authentication system

I am attempting to use the authentication system in firebase.js.
I am receiving the following error:
firebase__WEBPACK_IMPORTED_MODULE_6_.default.auth.onAuthStateChanged is not a function".
Here is my code:
import firebase from "firebase/app";
import "firebase/auth";
const firebaseConfig = {
apiKey: process.env.REACT_APP_API_KEY,
authDomain: process.env.REACT_APP_AUTHDOMAIN,
databaseURL: process.env.REACT_APP_BASEURL,
projectId: process.env.REACT_APP_PROJECT_ID,
storageBucket: process.env.REACT_APP_STORAGEBUCKET,
messagingSenderId: process.env.REACT_APP_MESSAGING_SENDER_ID,
appId: process.env.REACT_APP_APP_ID,
measurementId: process.env.REACT_APP_MEASUREMENT_IDenter code here
};
// Initialize Firebase
const fire= firebase.initializeApp(firebaseConfig);
const firebaseAuth = firebase.auth();
export default fire;
Also, I have imported the firebase file everywhere, but I still cannot access the auth functions.

Uncaught TypeError: Cannot read property 'initializeApp' of undefined

I'm trying to use a few Firebase libraries in my Vue project (I installed it using npm install firebase).
I added those in main.js:
import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'
Firebase.initializeApp({
apiKey: 'xxx',
authDomain: 'xxx',
databaseURL: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
measurementId: 'xxx'
})
And I get:
Uncaught TypeError: Cannot read property 'initializeApp' of undefined
Change this:
import { Firebase } from 'firebase/app'
into this:
import * as firebase from "firebase/app";
import everything from firebase/app then do:
firebase.initializeApp({
apiKey: 'xxx',
authDomain: 'xxx',
databaseURL: 'xxx',
projectId: 'xxx',
storageBucket: 'xxx',
messagingSenderId: 'xxx',
appId: 'xxx',
measurementId: 'xxx'
})
For newcomers using the newest Firebase Modular API (v9) a refactoring is required.
The imports must be changed to this:
import { FirebaseApp, initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getDatabase } from "firebase/database";
const firebaseConfig = {...}
// Initialize Firebase
const app: FirebaseApp = initializeApp(firebaseConfig);
const db = getDatabase(app);
const auth = getAuth(app);
About this new refactoring style here
And more information on how Read and Write data changed here
Check your Firebase version in your package.json file. If it is 9+ it should be something like this:
import { initializeApp } from "firebase/app";
import { getFirestore, collection, getDocs } from 'firebase/firestore/lite';
import { getAuth } from "firebase/auth";
and then:
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "...",
};
// Initialize Firebase
const firebase = initializeApp(firebaseConfig);
const db = getFirestore(firebase);
const auth = getAuth(firebase);
Error that appears caused by the difference of the version you use with the installed SDK version
Simply change from like this :
import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'
to this:
import { Firebase } from 'firebase/compat/app'
import 'firebase/compat/analytics'
import 'firebase/compat/auth'
import 'firebase/compat/messaging'
For clearer info, you can read this SDK9

Categories