无法将 aws-amplify 与 NextJS 13.4 api 路由一起使用

len*_*yai 6 api-gateway aws-amplify aws-amplify-sdk-js nextjs-dynamic-routing next.js13

我已成功在以前版本的 NextJS 中使用页面/api 路由连接到 AWS API Gateway。我正在尝试使用新的应用程序路由器在 app/api/route 中执行相同的操作。但我无法让aws-amplify图书馆工作,即

const {Auth: ssrAuth} = withSSRContext({ req: request });  //works
const user = await ssrAuth.currentAuthenticatedUser();     //fails
Run Code Online (Sandbox Code Playgroud)

这与页面路由器配合得很好,我能够获取用户令牌以将授权者附加到 API 网关请求。

知道为什么吗?请求就在那里,我可以看到带有用户令牌的 cookie。我究竟做错了什么?

非常感谢任何反馈/提示

len*_*yai 7

通常情况下,解决方案非常简单。答案深藏在 Amplify文档中

通过应用以下更改,Amplify JavaScript 可以与 Next.js App Router (Next.js v13.4+) 一起使用:

  1. 在客户端和服务器端代码中运行 Amplify.configure({ ...awsExports, ssr: true })

要将 Amplify 与 Next.js App Router 结合使用,您必须在客户端和服务器组件中运行 Amplify.configure()。必须启用 ssr 选项。

因此,就我而言,Amplify.configure当我使用 Auth Context 连接到 AWS Cognito 时,我已经在客户端上安装了。因此,现在我只需将相同的内容添加到每个路由文件使用的公共库中即可构建 axios 配置对象。它神奇地起作用了!显然,双方都需要单独访问环境变量。这有点道理,但如果有更好的文档和更多示例就更好了。