[英]How to use a next-auth jwt in relay-nextjs?
I am trying to use next-auth
with relay-nextjs
.我正在尝试将
next-auth
与relay-nextjs
使用。 I setup next-auth
per a hasura jwt example , and relay-nextjs
per the official docs .我根据hasura jwt 示例设置了
next-auth
,并根据官方文档设置了relay-nextjs
。 I want to pass a next-auth
jwt Bearer token in the relay-nextjs
Authorization header sent to the GraphQL endpoint.我想在发送到 GraphQL 端点的
relay-nextjs
授权 header 中传递next-auth
jwt 不记名令牌。
next-auth
provides a getSession
method. next-auth
提供了一个getSession
方法。 Under the hood , this requests a server endpoint. 在幕后,这会请求一个服务器端点。
relay-nextjs
provides a serverSideProps
property of a withRelay
HoC. relay-nextjs
提供了一个withRelay
HoC 的serverSideProps
属性。 You pass your page component to withRelay
.您将页面组件传递给
withRelay
。 You add a function to serverSideProps
that will send a token to the page's Relay environment.您将 function 添加到
serverSideProps
,它将向页面的 Relay 环境发送令牌。
My problem is getSession
is null inside serverSideProps
:我的问题是
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 };
}
If I can get the token here, it works in relay-nextjs
.如果我可以在这里获得令牌,它可以在
relay-nextjs
中使用。 Returning a string works fine, adds it to the header.返回字符串可以正常工作,将其添加到 header。
There's a next-auth
cookie with the app page request.应用页面请求中有一个
next-auth
cookie。 I checked it against the endpoint called by getSession
.我根据
getSession
的端点检查了它。 The cookies don't match. cookies 不匹配。 They do, until this part after the last dot, which changes on each request.
他们会这样做,直到最后一个点之后的这一部分,每个请求都会改变。
session-token=xxxx.xxxxxxxx.xxxxx
I ran it through the debugger, and it looks like relay-nextjs
executes before the next-auth
callback.我通过调试器运行它,看起来
relay-nextjs
next-auth
回调之前执行。
One approach I'm trying now is store the next-auth
token in a database, and run a prisma query instead of getSession
.我现在尝试的一种方法是将
next-auth
令牌存储在数据库中,并运行 prisma 查询而不是getSession
。 Any input is welcomed.欢迎任何意见。
ok this is a lot easier than I thought.好的,这比我想象的要容易得多。 Just do:
做就是了:
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'] };
}
I traced through the next-auth code here:我在这里跟踪了下一个身份验证代码:
https://github.com/nextauthjs/next-auth/blob/77012bc00cd4247ee633777916ece31976b2f477/src/server/routes/session.js#L25 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/lib/cookie.js#L142
https://github.com/nextauthjs/next-auth/blob/main/src/server/index.js#L66 https://github.com/nextauthjs/next-auth/blob/main/src/server/index.js#L66
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.