withAuth 模块未在 Next.js 的 next-auth 中导出

问题描述 投票:0回答:2

我试图在 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
reactjs next.js middleware next-auth
2个回答
1
投票

解决方案,更新你的nextAuth依赖。 withAuth 是 4.2.0 的更新。 https://github.com/nextauthjs/next-auth/releases/tag/v4.2.0 特别感谢juliomalves


0
投票

您的 next-auth 版本可能没有

withAuth

检查您的版本;你在

"beta"
/v5吗?这是他们在官方的 next-auth 示例中使用的版本(在撰写本文时)。

另请参阅:https://stackoverflow.com/a/77896110/19009041

© www.soinside.com 2019 - 2024. All rights reserved.