next.js getServerSession在下一个js 13上使用API路由在app目录上

zyfwsgd6  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(112)

我正在使用next-auth并试图在服务器组件上获取serverSession,我正在使用Next。js 13 beta的应用程序目录和API目录也在应用程序目录,所以根据下一个认证文档,这是我如何可以得到serverSession

import { getServerSession } from "next-auth/next"
import { authOptions } from "pages/api/auth/[...nextauth]"

export default async function Page() {
  const session = await getServerSession(authOptions)
  return <pre>{JSON.stringify(session, null, 2)}</pre>
}

但是AuthOptions不可用,因为根据next-auth文档,这是我的API路由在next中应该看起来像。js 13测试版

import NextAuth from "next-auth"

const handler = NextAuth({
  ...
})

export { handler as GET, handler as POST }

因此没有authOptions要导入的请求
我该怎么做
谢谢

p5cysglq

p5cysglq1#

您可以简单地按照以下方式导出authOptions和handler:

export const authOptions = {
   //...
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

相关问题