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
版权声明:本文标题:How to Retrieve User Data on Server-Side with VueFire, Nuxt, and Vercel? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.roclinux.cn/p/1741087369a2253271.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论