You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Auth更新后,如何在getServerSideProps中获取用户?

关于Firebase Hosting + Next.js 认证集成的疑问

文档中提到:

Express的res.locals对象可能会包含一个已认证的Firebase App实例(firebaseApp)和当前登录用户(currentUser),可在getServerSideProps中访问。

按照这个描述,我本应在getServerSideProps的context.res.locals中获取到Firebase App和用户实例,但实际测试时该值返回undefined:

export async function getServerSideProps(context) {
  // 输出undefined
  console.log(context.res.locals);
  return {
    props: {},
  };
}

文档后续补充说明:

已认证的Firebase App名称会在路由查询参数(__firebaseAppName)中提供,这允许在上下文内进行手动集成:

并给出代码示例:

// 获取已认证的Firebase App
const firebaseApp = getApp(useRouter().query.__firebaseAppName);

我对此感到困惑:这段代码的作用是什么?应该放置在哪个位置?我猜测这可能是让前一部分提到的res.locals能正常获取实例的必要步骤。

内容的提问来源于stack exchange,提问作者Alpharoah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 23:50:29