In my project under plugins dir I have a single plugins called firebase.ts
and it’s look like this
import { defineNuxtPlugin } from "#app";
import { firebaseConfig } from "@/firebaseConfig";
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
export default defineNuxtPlugin((nuxtApp) => {
// Initialize Firebase
const firebaseApp = initializeApp(firebaseConfig);
const firebaseAuthInstance = getAuth(firebaseApp);
})
Whenever I run my project it’s give this kind of error. But if I make the plugin client only i mean firebase.client.ts
then it work just fine. But I want to get this pluin both in client and server side. How to achive that?
[h3] [unhandled] H3Error: Component auth has not been registered yet
at createError (file:///home/riyad/Desktop/nuxt-test/node_modules/h3/dist/index.mjs:238:15)
at Server.nodeHandler (file:///home/riyad/Desktop/nuxt-test/node_modules/h3/dist/index.mjs:428:21) {
statusCode: 500,
fatal: false,
unhandled: true,
statusMessage: 'Internal Server Error'
}
[nuxt] [request error] Component auth has not been registered yet
at createError (./node_modules/h3/dist/index.mjs:238:15)
at Server.nodeHandler (./node_modules/h3/dist/index.mjs:428:21)
2
Answers
After many attempts, I was able to solve the issue. Here is the code on the directory /plugins/plugins.ts:
It appears that the initializeApp Function was not called correctly. But if you call the initializeApp and getAuth on a new function it returns The Auth Object correctly.
Firebase libraries use browser cache to store data on client side, so you cannot load some of their libraries on server side. Auth library is used and works only on client site, same as all lib’s with no
/lite
end. To be able to use library Firestore on server, import all functions from'firebase/firestore/lite'
file path.Example of plugin:
Usage:
Code directly in setup will run on server and client. Function
signIn
andif (process.client)
will run only on client same as life cycle hooks likeonMounted()