[英]How to use a next-auth jwt in relay-nextjs?
我正在尝试将next-auth
与relay-nextjs
使用。 我根据hasura jwt 示例设置了next-auth
,并根据官方文档设置了relay-nextjs
。 我想在发送到 GraphQL 端点的relay-nextjs
授权 header 中传递next-auth
jwt 不记名令牌。
relay-nextjs
提供了一个withRelay
HoC 的serverSideProps
属性。 您将页面组件传递给withRelay
。 您将 function 添加到serverSideProps
,它将向页面的 Relay 环境发送令牌。
我的问题是getSession
是 serverSideProps 内的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
中使用。 返回字符串可以正常工作,将其添加到 header。
应用页面请求中有一个next-auth
cookie。 我根据getSession
的端点检查了它。 cookies 不匹配。 他们会这样做,直到最后一个点之后的这一部分,每个请求都会改变。
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/main/src/server/lib/cookie.js#L142
https://github.com/nextauthjs/next-auth/blob/main/src/server/index.js#L66
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.