[Vuejs]-Firebase App not Initialized, NUXT plugin vs middleware

1👍

Instead of using a middleware, you can initialise Firebase admin in a separate file under server directory e.g. utils. Try:

// path: /server/utils/firebase.ts

import { cert, initializeApp, getApps, ServiceAccount } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';
import { getFirestore } from 'firebase-admin/firestore'; 

const app = getApps().length
  ? getApps()[0]
  : initializeApp({
      credential: cert({...}),
    });

export const authAdmin = getAuth(app);
export const firestoreAdmin = getFirestore(app); 

Then you can import authAdmin in your server middleware as shown below:

import { authAdmin } from '~/server/utils/firebase';
//...

Leave a comment