Having trouble with Firebase adapter for Next Auth in Next JS 13?

I have been working on setting up authentication in my Next JS 13 application using Next Auth and Firebase. However, I encountered an issue when running the code that resulted in the following error:

Error - FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists with different options or config (app/duplicate-app).

Interestingly, when I directly use the firebase config in the adapter, it works fine. But when I import it from a firebase.js file, the error occurs.

firebase.js

import { initializeApp, getApp, getApps } from "firebase/app";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";

const firebaseConfig = {
   apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
};

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

const db = getFirestore(app);
const storage = getStorage(app);

export { db, storage, app };

[...nextauth].js

import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import { FirestoreAdapter } from "@next-auth/firebase-adapter";
import { db } from "../../../firebase";

export default NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
  ],
  adapter: FirestoreAdapter(db),
  // ...
});

I would appreciate some help regarding this issue. Ever since upgrading to Next JS 13, it has been quite challenging.

One possible solution is to update the firebase.js as follows:

const firebaseConfig = {
   apiKey: process.env.FIREBASE_API_KEY,
authDomain: process.env.FIREBASE_AUTH_DOMAIN,
projectId: process.env.FIREBASE_PROJECT_ID,
storageBucket: process.env.FIREBASE_STORAGE_BUCKET,
messagingSenderId: process.env.FIREBASE_MESSAGING_SENDER_ID,
appId: process.env.FIREBASE_APP_ID,
};

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

export const db = getFirestore(app);
export const storage = getStorage(app);

The error seems to be associated with this part of the code in `[...nextauth].js`:

adapter: FirestoreAdapter(db), specifically related to the db entry.

Answer №1

In your code, the getApps function is being used incorrectly as you are not calling .length on it. Here's a suggestion:

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

// Incorrect: getApps.length    => 0 ✕
// Correct: getApps().length  => 1 ✔️

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to direct to dashboard if a user is logged in using AngularJS

As a newcomer to AngularJS, I am facing an issue with redirecting my page to the dashboard once the user has logged in. I receive an access token after login, which I save in cookies. Despite trying solutions from Stack Overflow, I have not been able to re ...

bindings and validation of input values in angularjs

In my scenario, I am dealing with a dynamic regExp and unique masks for each input. For instance, the regExp is defined as [0-9]{9,9} and the corresponding mask is XXX-XX-XX-XX. However, when it comes to Angular's pattern validation, this setup is con ...

`Error encountered when attempting to convert date with Angular JS filter`

Utilizing angular js filter in the controller for date formatting. A user selects a date from a uib-datepicker-popup. The goal is to format this date using ISO 8601 (yyyy-mm-dd) date format. Upon logging the user-selected date, the output is as follows: S ...

Incorporate create-react-app with Express

Issue - I am encountering a problem where I do not receive any response from Postman when trying to access localhost:9000. Instead of getting the expected user JSON data back, I am seeing the following output: <body> <noscript>You need to ...

Choose a unique text color

Having trouble with my HTML select form and modifying text color for specific options. In the provided example, I changed the color for the Tuesday option, but it only shows up when scrolling through the options. How can I make the color change visible for ...

Problem with alerting JSON String in AJAX response

I encountered a peculiar issue. When using PHP to run a select query on a MySQL database and return the result to my AJAX call, I face a problem where I receive "undefined" as an alert. Although I can alert the entire JSON string, I am unable to target sp ...

Ensure that the page is edited before being shown in the iframe

Is it possible to remove a div from an HTML page within an iFrame? The code below fetches an HTML page and displays it in an iframe. Is there a way to remove the specified div from the fetched HTML page? <script type="text/javascript"> (function(){ ...

Error in React: Attempting to access the 'year' property of an undefined value

I'm currently working on building a calendar using react hooks, but I've encountered an error that reads: Store.js:3661 Uncaught TypeError: Cannot read property 'year' of undefined at Function.diff (Store.js:3661) at Function.i ...

Unusual behavior in Javascript/PHP when using AJAX POST requests

When I send a post request to a PHP website using a function, I noticed that changing the capitalization of a specific variable called 'action' results in two different behaviors. If the 'action' variable is set to "deleteIndexmain," a ...

Having trouble deploying Firebase hosting with the latest version of Next.js, encountering an error

I started my next.js project by using the following command: npx create-next-app@latest testdemo During setup, I selected specific options for the creation of the project. Once the project was set up, it ran smoothly locally and the build process was succ ...

Managing MUI form fields using React

It seems like I may be overlooking the obvious, as I haven't come across any other posts addressing the specific issue I'm facing. My goal is to provide an end user with the ability to set a location for an object either by entering information i ...

Retrieve the text value from a react object

My current project involves utilizing the Last.fm API to retrieve artist information. Upon receiving the JSON object format, it appears like this: object format I am encountering an issue when trying to access the "#text" property within the React objec ...

How can I utilize `event.target` to specifically target sub-components and modify their styling?

Can we utilize the event.target property to specifically target and modify the style of sub-components? export default function CustomComponent(props) { function onMouseOver(event) { if (event.target.className === "container") { event.target.st ...

Toggle visibility of cards using bootstrap

I've been attempting to show and hide a selection of cards in bootstrap, but I'm having trouble figuring it out. All the cards share the class "card," and my goal is to hide them all when a specific button is clicked. Below is my current code: ...

What are your thoughts on the practice of utilizing the useState hook within a component to send data to its parent component?

I have been working on developing an Input component that can be dynamically used in any page to store input values. The component also includes an attribute called getValue, which allows the parent component to access the input value. In my App.js file, I ...

Solving the challenge of handling multiple text inputs in Laravel Blade using Vue.js v-model within a @

Hello, I am encountering an issue with my Laravel application that displays posts and comments. The problem lies with the Vue v-model in the input text when using the @foreach directive in Blade. Each post is showing the comments input box, but when I ente ...

Guide to retrieving JSON key and value using AJAX

Is there a way for me to retrieve my json object using the table header as the key and displaying the corresponding values in an HTML table? history:{1/22/20: 2, 1/23/20: 3, 1/24/20: 5, 2/1/20: 19, 2/10/20: 32, 2/11/20: 33, 2/12/20: 33, 2/2/20: 19, 2/20/2 ...

Experiencing difficulties loading webpages while attempting to execute Routes sample code using NodeJS

As a beginner in Javascript, I am attempting to execute the example code provided in the documentation for routes. The code snippet is as follows: var Router = require('routes'); var router = new Router(); router.addRoute('/admin/*?&apos ...

Spin an object along any axis in Three.js, similar to how the moon orbits the Earth

After attempting numerous solutions to the problem, the object continues to rotate around its own axis without any translation. ...

The "DELETE" method in ajax is malfunctioning

I encountered an "internal server error (500)" in the console. When checking my NodeJS console, I received a "ReferenceError: request is not defined" message. Below is the code snippet that caused the issue: $(document).ready(function(){ $('.dele ...