How to Initialize Firebase for Web App ?



Follow the steps to Setup and Initialize Web App :

  1. After Registering App in your Firebase you will get Firebase SDK.
  2. Using that firebase SDK, you are able to get services of firebase.
    Firebase SDK : 
    Firebase SDK
  3. If you don't have SDK after registering app in firebase then to find firebase SDK, click on 'setting icon' > 'Project Settings'
    Project Setting Button
    Now scroll down in dashboard of 'Project Settings' > 'General' tab and there is Firebase SDK and copy it.
    Firebase SDK
  4. Make a file named firebaseConfig.js and paste firebase SDK there in your project. [Make it in your projects 'root' directory or 'src' directory]

firebaseConfig.js file :
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: '<YOUR_API_KEY>',
  authDomain: '<YOUR_AUTH_DOMAIN>',
  databaseURL: '<YOUR_DATABASE_URL>',
  projectId: '<YOUR_PROJECT_ID>',
  storageBucket: '<YOUR_STORAGE_BUCKET>',
  messagingSenderId: '<YOUR_SENDER_ID>',
  appId: '<YOUR_APP_ID>',
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

Now initialize firebase services like authentication, realtime database, firestore, storage and many more like this way.
// Import the functions you need from the SDKs you need
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
import { getDatabase } from 'firebase/database';
import { getFirestore } from 'firebase/firestore';
import { getStorage } from 'firebase/storage';

const firebaseConfig = {
  apiKey: '<YOUR_API_KEY>',
  authDomain: '<YOUR_AUTH_DOMAIN>',
  databaseURL: '<YOUR_DATABASE_URL>',
  projectId: '<YOUR_PROJECT_ID>',
  storageBucket: '<YOUR_STORAGE_BUCKET>',
  messagingSenderId: '<YOUR_SENDER_ID>',
  appId: '<YOUR_APP_ID>',
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);

export const auth = getAuth(app); //Add to Initialize Authentication
export const database = getDatabase(app); //Add to Initialize Reatime Database
export const firestoreDB = getFirestore(app); //Add to Initialize Firestore Database
export const storage = getStorage(app); //Add to Initialize Storage

This is the best practice to initialize any service and export it so you can use in other files by simply importing it from this firebaseConfig.js file.

Related Post
Initialize app
How to Initialize Firebase for Web App ?
How to Initialize Firebase for Web App ?

1.Register App and get Firebase SDK 2.Add Firebase SDK to your app 3.Initialize it

21 October 2024

Create project
How to Create Firebase Project ?
How to Create Firebase Project ?

1.Visit firebase.google.com 2.Create/Add Project 3.Enter project name 4.Hit continue

21 October 2024

Register app
How to add Web App to Firebase Project ?
How to add Web App to Firebase Project ?

1.Open firebase project console & click on Web app icon 2.Enter app name 3.Click Register App button 4.Add SDK to your app

21 October 2024

Company
Explore

Copyright © 2024, All rights reserved by Firebase Master

We value your privacy

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Allow", you consent to our use of cookies. Here is our Privacy Policy.