API 路由中的中间件之后从 nextjs auth 获取 getServerSession

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

我正在使用 next-auth 和 nextjs 边缘函数。我正在使用边缘传输数据。

我有我的中间件

export { default } from "next-auth/middleware";

// Limit the middleware to paths starting with `/api/`
export const config = {
  matcher: "/api/:function*",
};

现在如何在 API 路由中访问会话对象?

因为我无法再访问正常的下一个请求、响应,而是获取 API

const handler = async (req: Request): Promise<Response> => {

// Below will fail
const session = await getServerSession(req, res, authOptions);

感谢任何帮助,谢谢!

next.js middleware server-side-rendering next-auth next.js13
2个回答
0
投票

您向

getServerSession
传递了错误的参数。这就是它的类型

getServerSession<AuthOptions, Session>(...args: GetServerSessionParams<AuthOptions>): Promise<Session | null>

我在这里解释了 authOptions:如何在应用程序目录中的 next js 13 beta 服务器组件中使用 next auth getServerSession


0
投票

我知道的不多。但就我而言,我就是这样使用的。 在此输入链接描述

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