How do I access vuefire in a Nuxt 3 Server Route? #1343
Replies: 6 comments 7 replies
-
Since I had the same question as you, and there has been no answer to this question yet, I will share how I solved it. Not sure if it's the right way to do it, but it works for me. As far as I know, the Vuefire object is only available in the Vue part of the app and not in the server routes. So I created a useFirebaseServer composable in 'server/api/utils', where it checks if there already is an initialized app available and if not, a new Firebase app is initialized. // server/utils/useFirebaseServer.js
import {initializeApp, getApps, cert} from "firebase-admin/app";
export default function useFirebaseServer() {
if (getApps().length == 0)
initializeApp({
credential: cert('./firebase-credentials.json') // path to service account credentials file
});
} Then you can use this composable in the server route that you want: // server/api/route.js
import useFirebaseServer from "~/server/utils/useFirebaseServer";
export default defineEventHandler( (event) => {
useFirebaseServer(); // this will initialize Firebase app if not initialized yet
// now you can use getAuth, getFirestore etc. from the firebase-admin SDK
}) |
Beta Was this translation helpful? Give feedback.
-
also i noticed that /api/__session session needs to be exposed, i had a middleware that was killing all routes except for few specified routes, but when using session cookies i believe vuefire calls /api/__session |
Beta Was this translation helpful? Give feedback.
-
hey @posva i had a server middleware that checks if usertoken (non firebase) exists on certain routes, however i did not know about __session and because of that i would raise Unauthorised before the request reaches the actual path.. I am still learning about nuxt so I might be doing it completely wrong, but at the moment I have to manually check the route that is being called and ensure i skip those routes from being rejected. but this did not seem to be the problem.
I posted the error here. This lead me to believe that I had incorrect permission , which was really strange because it was working before and I did not make any changes to firebase. I spent many hours trying to figuring this out . But it seems like it wasn't a firebase configuration issue. I finally dug into the __session route it self to see where "Error minting the cookie" is coming from. I double checked the .env file so i decided to comment out the ensureAdminApp portion
and i replace it with the above,
This seems to have fixed the issue. As the sessioncookies are now being created and stored in cookie without error. So I have no idea why it broke in the first place and why this fixes it. Do you have any ideas ? Thanks |
Beta Was this translation helpful? Give feedback.
-
also noted if you dont put GOOGLE_APPLICATION_CREDENTIALS= in the .env file the __session route does not become integrated. |
Beta Was this translation helpful? Give feedback.
-
So ... when i ran the production version with DEBUG using DEBUG=* node .output/server/index.mjs. I noticed that the GOOGLE_APPLICATION_CREDENTIALS is empty. Even the _inlineRuntimeConfig parses the .env file , I checked the .output server chunks and it had these So I thought i would export GOOGLE_APPLICATION_CREDENTIALS='ActualServiceAccountObjectGoesHere' and then it works .. what could be the problem here ?? |
Beta Was this translation helpful? Give feedback.
-
So .env files are not read at production point ( so process.env won't detect anything during server runtime) , side note: they are read at build hence why the runtimeconfig is populated. https://nuxt.com/docs/guide/directory-structure/env So to run in production you have to do the following: @posva can I make a request to have this added for other newbies like me for production. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to write a server route that renders the documents from a firestore query as an RSS feed. I'm having a very hard time understanding how to actually access the Firestore object when working inside a server route. Extrapolating out from the middleware example in the documentation, the code should look something like this:
This doesn't work, failing with
Firebase: Need to provide options, when not being deployed to hosting via source. (app/no-options).
Nuxt has access to a service account and SSR should be working. What am I not understanding?
Beta Was this translation helpful? Give feedback.
All reactions