我正在尝试将next-auth
与relay-nextjs
一起使用。我根据hasura jwt示例设置了next-auth
,根据官方文档设置了relay-nextjs
。我想在发送到GraphQL端点的relay-nextjs
Authorization标头中传递一个next-auth
jwt Bearer令牌。
-
next-auth
提供了一种getSession
方法。在后台,这会请求一个服务器端点。 -
CCD_ 9提供了CCD_ 11 HoC的CCD_。您将页面组件传递给
withRelay
。您可以向serverSideProps
添加一个函数,该函数将向页面的中继环境发送一个令牌。
我的问题是getSession
在serverSideProps
:内为空
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-auth
cookie。我对照getSession
调用的端点对其进行了检查。饼干不匹配。他们会这样做,直到最后一个点之后的这一部分,每个请求都会更改。
session-token=xxxx.xxxxxxxx.xxxxx
我通过调试器运行了它,看起来relay-nextjs
在next-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