Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
NextAuth Firebase Backend "ReferenceError: Cannot access 'app' before initialization"

I'm trying to store users on a firebase backend when using next auth I can't get around this error

Using next-auth FIREBASE adapter.

https://next-auth.js.org/adapters/firebase :DOCS IM FOLLOWING

FIREBASE CLIENT

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

// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
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_MEASURMENT_ID,
};

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

export default firebase;

[...nextauth].js

import NextAuth from "next-auth";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
import firebaseClient from "../../../firebase/FirebaseClient";
import GoogleProvider from "next-auth/providers/google";

import firebase from "firebase/app";
import "firebase/firestore";

const firestore = (firebase.apps[0] ?? firebaseClient).firestore();

export default NextAuth({
  // Configure one or more authentication providers
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    }),
  ],
  adapter: FirebaseAdapter(firestore),
});

err

ReferenceError: Cannot access 'app' before initialization
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I bet you need to switch lines as following, because you don't have app variable when you create analytics:

const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
about 4 years ago · Juan Pablo Isaza Report

0

Alternative Problem: Internally-referenced Functions

Our issue was a bit different. If you've developed a cloud function that has an internally-referenced function, like this:

export.foo = functions.https.onCall(async () => { 
// Some functionality here that references internalFunc

const internalFunc = () => { ... }
}

That internalFunc won't be able to be referenced.

The solution for us is to create a utilities.js file in our functions directory, then export the internalFunc from that file. Then, import that function to the script where you're deploying the cloud function.

Hope this helps someone!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!