当我试着用
adapter: FirebaseAdapter(firestore)来自-> https://next-auth.js.org/adapters/firebase
,在firebase之后,v9下一个-auth hasen的文档没有更新.
我得到了这个错误:TypeError:不是一个函数
我的守则:
Api密钥,客户端秘密已被dots替换(.)藏起来。
import NextAuth from "next-auth";
import Providers from "next-auth/providers";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
import { getFirestore } from "firebase/firestore";
import { initializeApp, getApps } from "firebase/app";
import "firebase/firestore";
const firebaseConfig = {
apiKey: "...",
authDomain: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appId: "...",
measurementId: "....",
};
const app = initializeApp(firebaseConfig);
const firestore = getFirestore(app);
const options = {
providers: [
Providers.Google({
clientId:
"...",
clientSecret: "...",
}),
],
adapter: FirebaseAdapter(firestore),
// database: process.env.MONGODB_URI,
};
export default (req, res) => {
NextAuth(req, res, options);
};发布于 2021-12-24 04:23:27
我使用以下方法设法解决了同样的错误:
1-当firebase v9遵循模块化方法时,将析构FirebaseClient对象传递给火基适配器,如下所示
// pages/api/auth/[...nextauth].js
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";
import { FirebaseAdapter } from "@next-auth/firebase-adapter";
import { db } from "../../../firebase";
import {
collection,
query,
getDocs,
where,
limit,
doc,
getDoc,
addDoc,
updateDoc,
deleteDoc,
runTransaction,
} from "firebase/firestore";
export default NextAuth({
providers: [
// OAuth authentication providers
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
GithubProvider({
clientId: "",
clientSecret: "",
}),
],
adapter: FirebaseAdapter({
db,
collection,
query,
getDocs,
where,
limit,
doc,
getDoc,
addDoc,
updateDoc,
deleteDoc,
runTransaction,
}),
});
2-然后您可能会得到一个错误,即“不能使用未定义的toDate方法”,如果这发生在index.js文件中的node_modules/@next-auth/firebase-适配器/dist中,则用添加"?“的代码替换exports.format对象。运算符后值对象
exports.format = {
toFirestore(object) {
const newObjectobject = {};
for (const key in object) {
const value = object[key];
if (value === undefined)
continue;
newObjectobject[key] = value;
}
return newObjectobject;
},
fromFirestore(snapshot) {
if (!snapshot.exists())
return null;
const newUser = { ...snapshot.data(), id: snapshot.id };
for (const key in newUser) {
const value = newUser[key];
if (value?.toDate)
newUser[key] = value.toDate();
else
newUser[key] = value;
}
return newUser;
},
};
最后一件事是在package.json中删除下一个版本之前的"^“,以便在执行npm安装时禁用包更新,这将重置步骤2中发生的所有代码更改。
此解决方案假定您有firebase.js文件,该文件应如下所示
import { initializeApp, getApps, getApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";
import { getStorage } from "firebase/storage";
const firebaseConfig = {
apiKey: "your_api_key",
authDomain: "your_auth_domain",
projectId: "your_project_Id",
storageBucket: "your_storage_bucket",
messagingSenderId: "message_sender_Id",
appId: "your_app_Id",
};
// Initialize Firebase
const app = getApps.length > 0 ? getApp() : initializeApp(firebaseConfig);
const db = getFirestore(app);
const storage = getStorage(app);
export { app, db, storage };
希望这能解决你的问题。
https://stackoverflow.com/questions/69876727
复制相似问题