如何修复 Firebase admin verifyIdToken 不是函数
How to fix Firebase admin verifyIdToken is not a function
我正在解决 Lee Robinson's React 2025 course and I'm experiencing an issue while trying to verify an ID token through Firebase admin. The closest issue I can find is this 但解决方案对我不起作用。
我得到的错误是:
TypeError: firebase_admin__WEBPACK_IMPORTED_MODULE_1__.auth.verifyIdToken is not a function
at __webpack_exports__.default (webpack-internal:///./pages/api/notes.js:10:65)
at apiResolver (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/api-utils.js:8:7)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async DevServer.handleApiRequest (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:63:403)
at async Object.fn (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:55:530)
at async Router.execute (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/router.js:24:67)
at async DevServer.run (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:65:1023)
at async DevServer.handleRequest (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:33:450)
相关文件如下:
firebase-admin.js
import admin from 'firebase-admin';
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert({
project_id: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY,
client_email: process.env.FIREBASE_CLIENT_EMAIL,
}),
databaseURL: '<my database url, removed for this post>',
});
}
const db = admin.firestore();
const auth = admin.auth();
export { db, auth };
notes.js
import { getUserNotes } from '@/lib/db-admin';
import { auth } from 'firebase-admin';
export default async (req, res) => {
const { uid } = await auth.verifyIdToken(req.headers.token);
const { notes } = await getUserNotes(uid);
res.status(200).json({ notes });
};
我认为我的 Firebase admin SDK 设置没有问题,因为如果我删除 verifyIdToken
方法并直接插入 uid
,我可以从数据库中获取数据。
如果我需要提供更多信息,请告诉我。谢谢!
有人向我指出notes.js中的导入应该是相对文件路径导入,而不是导入节点包。这修复了错误!
所以应该是:
import { auth } from ‘@/utils/firebase-admin’;
在 notes.js.
我正在解决 Lee Robinson's React 2025 course and I'm experiencing an issue while trying to verify an ID token through Firebase admin. The closest issue I can find is this 但解决方案对我不起作用。
我得到的错误是:
TypeError: firebase_admin__WEBPACK_IMPORTED_MODULE_1__.auth.verifyIdToken is not a function
at __webpack_exports__.default (webpack-internal:///./pages/api/notes.js:10:65)
at apiResolver (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/api-utils.js:8:7)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async DevServer.handleApiRequest (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:63:403)
at async Object.fn (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:55:530)
at async Router.execute (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/router.js:24:67)
at async DevServer.run (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:65:1023)
at async DevServer.handleRequest (/Users/Alfred/Documents/Alfred/Coding/augment-notes/augment-notes/node_modules/next/dist/next-server/server/next-server.js:33:450)
相关文件如下:
firebase-admin.js
import admin from 'firebase-admin';
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert({
project_id: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
private_key: process.env.FIREBASE_PRIVATE_KEY,
client_email: process.env.FIREBASE_CLIENT_EMAIL,
}),
databaseURL: '<my database url, removed for this post>',
});
}
const db = admin.firestore();
const auth = admin.auth();
export { db, auth };
notes.js
import { getUserNotes } from '@/lib/db-admin';
import { auth } from 'firebase-admin';
export default async (req, res) => {
const { uid } = await auth.verifyIdToken(req.headers.token);
const { notes } = await getUserNotes(uid);
res.status(200).json({ notes });
};
我认为我的 Firebase admin SDK 设置没有问题,因为如果我删除 verifyIdToken
方法并直接插入 uid
,我可以从数据库中获取数据。
如果我需要提供更多信息,请告诉我。谢谢!
有人向我指出notes.js中的导入应该是相对文件路径导入,而不是导入节点包。这修复了错误!
所以应该是:
import { auth } from ‘@/utils/firebase-admin’;
在 notes.js.