我正在尝试使用next-auth
with relay-nextjs
。我next-auth
按照hasura jwt 示例relay-nextjs
和官方文档进行设置。我想在发送到 GraphQL 端点next-auth
的 Authorization 标头中传递一个 jwt Bearer 令牌。relay-nextjs
relay-nextjs
提供了serverSideProps
一个withRelay
HoC 的属性。您将页面组件传递给withRelay
. 您添加一个函数serverSideProps
,它将向页面的中继环境发送一个令牌。
我的问题getSession
是 null inside 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
。cookie 不匹配。他们会这样做,直到最后一个点之后的这一部分,每个请求都会改变。
session-token=xxxx.xxxxxxxx.xxxxx
我通过调试器运行它,它看起来像在回调relay-nextjs
之前执行。next-auth
我现在尝试的一种方法是将next-auth
令牌存储在数据库中,然后运行 prisma 查询而不是getSession
. 欢迎任何意见。