传递令牌与SSR重定向

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

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 注销页面。

return {
  redirect: {
    permanent: false,
    destination: "/api/auth/signout"
  }
}

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,例如:

const csrfToken = await getCsrfToken({ req })

return {
  redirect: {
    method: "POST",
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
    },
    body: csrfToken,
    permanent: false,
    destination: "/api/auth/signout"
  }
}

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.

    return {
      redirect: {
        permanent: false,
        destination: "/api/auth/signout"
      }
    }

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.

const csrfToken = await getCsrfToken({ req })
   
     return {
          redirect: {
            method: "POST",
            headers: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
            },
            body: csrfToken,
            permanent: false,
            destination: "/api/auth/signout"
          }
        }

How should I pass in the csrfToken in the redirect?

答案1

得分: 1

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

export type Redirect =
  | {
      statusCode: 301 | 302 | 303 | 307 | 308
      destination: string
      basePath?: false
    }
  | {
      permanent: boolean
      destination: string
      basePath?: false
    }

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

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

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

export type Redirect =
  | {
      statusCode: 301 | 302 | 303 | 307 | 308
      destination: string
      basePath?: false
    }
  | {
      permanent: boolean
      destination: string
      basePath?: false
    }

you can use context.res.writeHead

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:

确定