Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
11
Help us understand the problem. What is going on with this article?
@taskeknight

Firebaseで「Firebase App named '[DEFAULT]' already exists」というエラーがでた

認証にFirebase Authenticationを使ってみたのですが、

FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists 

何回かログインを繰り返していると、こんなエラーがコンソールに出力されて動かなくなります。
なんでしょう。
Firebaseのバージョンは7.15.5となっております。

↓Firebaseはこのような形で初期化しています。

firebase.ts
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/firebase-storage";
import "firebase/auth";

// firebaseコンソールから取得
const firebaseConfig = {
  apiKey: "xxxxx",
  authDomain: "sample.firebaseapp.com",
  databaseURL: "https://sample.firebaseio.com",
  projectId: "sample",
  storageBucket: "sample.appspot.com",
  messagingSenderId: "344403761159",
  appId: "yyyyy",
  measurementId: "zzzzz",
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);

export default firebase;

export const auth = firebase.auth();

firebase.initializeAppが複数回呼ばれていることが原因でしたので、

firebase.ts
// Initialize Firebase
if (firebase.apps.length === 0) {
  firebase.initializeApp(firebaseConfig);
}

初期化の部分をこのように修正いたしました。
無事コンソールにエラーメッセージは出なくなりました。

参考

if (!firebase.apps.length) {
firebase.initializeApp({});
}
https://stackoverflow.com/questions/43331011/firebase-app-named-default-already-exists-app-duplicate-app

11
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
taskeknight
(本当はjavaが好きです)

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
11
Help us understand the problem. What is going on with this article?