如何在relay-nextjs中使用下一个auth-jwt



我正在尝试将next-authrelay-nextjs一起使用。我根据hasura jwt示例设置了next-auth,根据官方文档设置了relay-nextjs。我想在发送到GraphQL端点的relay-nextjsAuthorization标头中传递一个next-authjwt Bearer令牌。

  1. next-auth提供了一种getSession方法。在后台,这会请求一个服务器端点。

  2. CCD_ 9提供了CCD_ 11 HoC的CCD_。您将页面组件传递给withRelay。您可以向serverSideProps添加一个函数,该函数将向页面的中继环境发送一个令牌。

我的问题是getSessionserverSideProps:内为空

serverSideProps: async (ctx) => {
const { getSession } = await import('next-auth/client');
// This is an example of getting an auth token from the request context.
// If you don't need to authenticate users this can be removed and return an
// empty object instead.
const token = await getSession();
return { token };
}

如果我能在这里获得令牌,它在relay-nextjs中工作。返回字符串效果良好,将其添加到标头中。

应用程序页面请求中有一个next-authcookie。我对照getSession调用的端点对其进行了检查。饼干不匹配。他们会这样做,直到最后一个点之后的这一部分,每个请求都会更改。

session-token=xxxx.xxxxxxxx.xxxxx

我通过调试器运行了它,看起来relay-nextjsnext-auth回调之前执行。

我现在尝试的一种方法是将next-auth令牌存储在数据库中,并运行prisma查询而不是getSession。欢迎任何意见。

好吧,这比我想象的要容易得多。只需:

serverSideProps: async (ctx) => {
// This is an example of getting an auth token from the request context.
// If you don't need to authenticate users this can be removed and return an
// empty object instead.
return { token: ctx.req.cookies['next-auth.session-token'] };
}

我在这里追踪到了下一个身份验证代码:

https://github.com/nextauthjs/next-auth/blob/77012bc00cd4247ee633777916ece31976b2f477/src/server/routes/session.js#L25

https://github.com/nextauthjs/next-auth/blob/main/src/server/lib/cookie.js#L142

https://github.com/nextauthjs/next-auth/blob/main/src/server/index.js#L66

相关内容

最新更新