admin 管理员组

文章数量: 1184232

I'm trying to retrieve authenticated user data on the server side using VueFire and Nuxt (SSR). Authentication works fine on the client side (CSR), and I can see the __session cookie being generated. I also manually verified the token using a script, and it works correctly. However, I want to use VueFire’s built-in functionality instead of handling authentication manually.

When calling await useCurrentUser(); in a server API route, I get inconsistent results:

Sometimes, it returns an object but without user data. Other times, it throws the following error:

[nuxt] [request error] [unhandled] [500] Firebase: No Firebase App '[DEFAULT]' has been created - call initializeApp() first (app/no-app).

I'm using Vercel for hosting (deployed via Vercel CLI) and the following dependencies:

"firebase": "^11.3.1",
"firebase-admin": "^13.1.0",
"firebase-functions": "^6.3.2",
"nuxt": "^3.15.4",
"nuxt-vuefire": "^1.0.5",
"vuefire": "^3.2.1"

All Firebase credentials are stored in .env, and they work in CSR. I also successfully retrieved the token manually from GOOGLE_APPLICATION_CREDENTIALS.

My nuxt.config.ts has authentication and cookies enabled:

export default defineNuxtConfig({
  compatibilityDate: "2024-11-01",
  devtools: { enabled: true },
  ssr: true,
  modules: ["nuxt-vuefire"],
  vuefire: {
    auth: {
      enabled: true,
      sessionCookie: true,
    },
    config: {
      apiKey: process.env.NUXT_PUBLIC_GOOGLE_FIREBASE_CONFIG_API_KEY,
      authDomain: process.env.NUXT_PUBLIC_GOOGLE_FIREBASE_CONFIG_AUTH_DOMAIN,
      projectId: process.env.NUXT_PUBLIC_GOOGLE_FIREBASE_CONFIG_PROJECT_ID,
      appId: process.env.NUXT_PUBLIC_GOOGLE_FIREBASE_CONFIG_APP_ID,
    },
  },
});

To fetch user data server-side, I created an API route in server/api/me.ts:

import { useCurrentUser } from "vuefire";
export default defineEventHandler(async (event) => {
  const user = await useCurrentUser();
  console.log("            
            
            

本文标签: How to Retrieve User Data on ServerSide with VueFire nuxt and VercelStack Overflow