带有 auth0 库的 Next.js App Router 会导致错误:TypeError:无法读取未定义的属性(读取“标头”)

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

我正在使用这个库实现 auth0

"@auth0/nextjs-auth0": "^3.0.1"

我在这个路径下做了一个route.ts文件

/app/api/auth/[auth0]/route.ts

import { handleAuth, handleLogin, handleLogout } from '@auth0/nextjs-auth0';
import { NextApiRequest, NextApiResponse } from 'next';

export const GET = handleAuth({
  async login(req: NextApiRequest, res: NextApiResponse) {
    await handleLogin(req, res, {
      returnTo: `http://localhost:3000/home`,
    });
  },
    async logout(req: NextApiRequest, res: NextApiResponse) {
      await handleLogout(req, res, {
        returnTo: `http://localhost:3000/home`,
      });
    },
});

export const dynamic = 'force-dynamic';

这不起作用。当我尝试登录时出现错误。错误是

- error TypeError: Cannot read properties of undefined (reading 'headers')
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:265:61)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

有什么建议吗?

next.js auth0
1个回答
0
投票

我使用 Nextjs 13.3 创建了一个网站,并使用 Javascript... 对于 Auth Api,我使用页面,就好像我能够正确回忆起来,当我尝试使用应用程序路由器进行身份验证时,我遇到了同样的问题。 我可以说,通过使用应用程序路由器进行网站和页面路由器进行身份验证,身份验证工作得非常完美。我在我的项目中使用 Google Auth,您可以根据您对 TypeScript 的要求和身份验证方法来编辑和添加一些内容。

pages/api/auth/[...nextauth].js

import NextAuth from "next-auth"
import GoogleProvider from "next-auth/providers/google";
import { MongoDBAdapter } from "@next-auth/mongodb-adapter"
import clientPromise from "../../../utils/db"

export const authOptions = {
  
  adapter: MongoDBAdapter(clientPromise),
  session: {
    secret: process.env.SECRET,
    jwt: true,  
  },
  providers: [
    // OAuth authentication providers
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET
    })
  ],
  secret: process.env.SECRET,
  callbacks: {
    async session({ session, token, user }) {
      // Send properties to the client, like an access_token and user id from a provider.
      // This was for my own purpose
      if (user.admin){
        session.user.admin = user.admin
      }
      return session
    }
  }
}

export default NextAuth(authOptions)

如果您想要 Mongo 适配器和客户端承诺的代码,请通知我

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