OneCompiler

Add Firebase to NextJS

385


In this post, we will see how to add firebase to NextJS

Installation

Run the below command to install firebase

yarn add firebase

Usage

Create a firebase.js file in the root directory and paste the code

import firebase from "firebase/app";
import "firebase/auth";

if (typeof window !== "undefined" && !firebase.apps.length) {
  firebase.initializeApp({
    apiKey: "XXXXXXXXX",
    authDomain: "XXXXXXXXX",
    databaseURL: "XXXXXXXXX",
    projectId: "XXXXXXXXX",
    storageBucket: "XXXXXXXXX",
    messagingSenderId: "XXXXXXXXX",
    appId: "XXXXXXXXX",
    measurementId: "XXXXXXXXX",
  });
}

export { firebase };

NOTE: Replace the XXXXX with your credentials

If you want to use firebase inside a component, use the below syntax to import it

import { firebase } from '../firebase'