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

Firebase v9下使用Next-auth的Firebase Adapter报collection is not a function错误

问题解决:Next-auth FirebaseAdapter适配Firebase v9报错TypeError: collection is not a function

错误原因

官方默认发布的@next-auth/firebase-adapter早期版本仅适配Firebase v8及更低版本的命名空间式API,Firebase v9采用全新模块化API,取消了原有挂载在实例上的collection()等方法,二者调用逻辑不兼容,因此adapter运行时会抛出方法不存在的错误。

解决方案

方案1:使用Firebase v9兼容层(改动最小,无需调整其他业务代码)

Firebase v9自带向后兼容层,只需调整Firebase相关导入方式,即可生成适配v8 API风格的Firestore实例,直接对接官方原有adapter:

import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
// 替换为compat兼容模式导入
import { initializeApp } from "firebase/compat/app";
import "firebase/compat/firestore";

const firebaseConfig = {
  apiKey: "...",
  authDomain: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "...",
  appId: "...",
  measurementId: "....",
};

const app = initializeApp(firebaseConfig);
// 获取兼容v8规范的Firestore实例
const firestore = app.firestore();

const options = {
  providers: [
    Providers.Google({
      clientId: "...",
      clientSecret: "...",
    }),
  ],
  adapter: FirebaseAdapter(firestore),
};

export default (req, res) => NextAuth(req, res, options);

该方案不影响项目其他模块使用Firebase v9的模块化API,适配成本最低。

方案2:升级依赖到原生适配版本

将next-auth升级到v4及以上版本,同时将@next-auth/firebase-adapter升级到v1.0.0及以上版本,新版adapter原生支持Firebase v9模块化API,按新版规范调整配置代码即可。注意该方案需要适配Next-auth v4的API Breaking Change,比如Provider的导入方式、配置结构调整等。

方案3:自定义适配v9的Adapter

如果不想使用兼容层也不想升级Next-auth版本,可以参考官方Adapter的逻辑,自行改写所有数据库操作调用为Firebase v9模块化写法,比如用collection(firestore, "集合名")替换原有firestore.collection("集合名")的调用,完成适配。

内容的提问来源于stack exchange,提问作者Charlie Østergaard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 08:24:00