传递令牌与SSR重定向

huangapple go评论96阅读模式
英文:

Pass token with ssr redirect

问题

I am using NextAuth and I need to sign out the user within the ssr method getServerSideProps. To achieve this I am simply returning a redirect to the NextAuth signout URL. This works fine except I want the user to signout automatically without opening the fallback NextAuth signout page.

我正在使用 NextAuth,我需要在 ssr 方法 getServerSideProps 中注销用户。为了实现这个目标,我只需返回一个重定向到 NextAuth 注销 URL。这个方法运行良好,除非我希望用户自动注销,而不打开 NextAuth 注销页面。

  1. return {
  2. redirect: {
  3. permanent: false,
  4. destination: "/api/auth/signout"
  5. }
  6. }

So I tried to pass in the csrftoken but I am not sure how to set it to the body in the nextjs redirect context. e.g.

因此,我尝试传递 csrfToken,但我不确定如何将其设置为 nextjs 重定向上下文中的 body,例如:

  1. const csrfToken = await getCsrfToken({ req })
  2. return {
  3. redirect: {
  4. method: "POST",
  5. headers: {
  6. 'Accept': 'application/json',
  7. 'Content-Type': 'application/json',
  8. },
  9. body: csrfToken,
  10. permanent: false,
  11. destination: "/api/auth/signout"
  12. }
  13. }

How should I pass in the csrfToken in the redirect?

在重定向中如何传递 csrfToken?

英文:

I am using NextAuth and I need to sign out the user within the ssr method getServerSideProps. To achieve this I am simply returning a redirect to the NextAuth signout URL. This works fine except I want the user to signout automatically without opening the fallback NextAuth signout page.

  1. return {
  2. redirect: {
  3. permanent: false,
  4. destination: "/api/auth/signout"
  5. }
  6. }

So I tried to pass in the csrftoken but I am not sure how to set it to the body in the nextjs redirect context. e.g.

  1. const csrfToken = await getCsrfToken({ req })
  2. return {
  3. redirect: {
  4. method: "POST",
  5. headers: {
  6. 'Accept': 'application/json',
  7. 'Content-Type': 'application/json',
  8. },
  9. body: csrfToken,
  10. permanent: false,
  11. destination: "/api/auth/signout"
  12. }
  13. }

How should I pass in the csrfToken in the redirect?

答案1

得分: 1

你不能使用redirect来传递标头。以下是它的类型:

  1. export type Redirect =
  2. | {
  3. statusCode: 301 | 302 | 303 | 307 | 308
  4. destination: string
  5. basePath?: false
  6. }
  7. | {
  8. permanent: boolean
  9. destination: string
  10. basePath?: false
  11. }

你可以使用context.res.writeHead方法:

  1. ServerResponse<IncomingMessage>.writeHead(statusCode: number, statusMessage?: string | undefined, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined): ServerResponse<IncomingMessage>
英文:

you cannot pass headers with redirect. this is its type:

  1. export type Redirect =
  2. | {
  3. statusCode: 301 | 302 | 303 | 307 | 308
  4. destination: string
  5. basePath?: false
  6. }
  7. | {
  8. permanent: boolean
  9. destination: string
  10. basePath?: false
  11. }

you can use context.res.writeHead

  1. ServerResponse&lt;IncomingMessage&gt;.writeHead(statusCode: number, statusMessage?: string | undefined, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined): ServerResponse&lt;IncomingMessage&gt;

huangapple
  • 本文由 发表于 2023年5月13日 21:28:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定