Next-auth FirestoreAdapter无法连接后端问题求助
尝试通过GoogleProvider实现NextAuth认证并存储用户信息,但应用无法连接后端。已将Firestore安全规则设为全开放,问题仍存在。
[...nextauth].js代码
import { FirestoreAdapter } from '@next-auth/firebase-adapter'; import NextAuth from 'next-auth'; import GoogleProvider from 'next-auth/providers/google'; import { app, db } from '../../../config/firebase'; export default NextAuth({ providers: [ GoogleProvider({ clientId: process.env.GOOGLE_CLIENT_ID, clientSecret: process.env.GOOGLE_CLIENT_SECRET, }), ], adapter: FirestoreAdapter({ apiKey: '...', authDomain: '...', projectId: '...', storageBucket: '...', databaseURL: '...', messagingSenderId: '...', appId: '...', }), debug: true, });
firebase.js代码
import { initializeApp, getApp, getApps } from 'firebase/app'; import { getAuth } from 'firebase/auth'; import { getFirestore } from 'firebase/firestore'; import { initializeFirestore } from 'firebase/firestore'; // Your web app's Firebase configuration const firebaseConfig = { apiKey: '...', authDomain: '...', projectId: '...', storageBucket: '...', messagingSenderId: '...', appId: '...', measurementId: '...', }; const app = initializeApp(firebaseConfig); const auth = getAuth(app); const db = getFirestore(app); export { app, auth, db };
Firestore安全规则
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if true; } } }
错误信息
@firebase/firestore: Firestore (9.12.1): 无法连接Cloud Firestore后端。连接失败1次。最新错误:FirebaseError: [code=permission-denied]: 权限被拒绝:消费者'project:undefined'已被暂停。
排查方向及解决方法
检查Firebase项目状态
错误提示project:undefined被暂停,首先确认Google Cloud控制台中,Firebase关联的GCP项目是否正常(未欠费、未被封禁);同时确保FirestoreAdapter和firebase.js里的projectId已填入真实值,不要留占位符...。简化FirestoreAdapter配置
已经在firebase.js中初始化了Firestore实例,无需在NextAuth的adapter里重复填写完整配置,直接复用已初始化的db实例即可:
修改[...nextauth].js的adapter部分:adapter: FirestoreAdapter(db),避免重复配置导致参数不一致。
核对配置参数一致性
确保firebase.js里的firebaseConfig所有字段都正确填充,特别是projectId;同时确认GoogleProvider的clientId和clientSecret对应正确的Firebase项目,且已在Google Cloud控制台启用Google OAuth服务。确认Firestore服务状态
检查Firebase控制台,确认Firestore数据库已创建并完成初始化,未处于未启用状态。
内容的提问来源于stack exchange,提问作者LonelySoul

