我试图在 next.js 的 /admin 路径中添加一个中间件,在文件“_middleware.js”中它是这样的:
import { withAuth } from "next-auth/middleware";
export default withAuth({
callbacks: {
authorized({ req, token }) {
if (token) return true; // If there is a token, the user is authenticated
},
},
});
错误:
./pages/admin/_middleware.js:1:0
Module not found: Package path ./middleware is not exported from package
路径:
pages
|-------admin
|--------------_middleware.js
|--------------index.js
解决方案,更新你的nextAuth依赖。 withAuth 是 4.2.0 的更新。 https://github.com/nextauthjs/next-auth/releases/tag/v4.2.0 特别感谢juliomalves
您的 next-auth 版本可能没有
withAuth
。
检查您的版本;你在
"beta"
/v5吗?这是他们在官方的 next-auth 示例中使用的版本(在撰写本文时)。