英文:
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<IncomingMessage>.writeHead(statusCode: number, statusMessage?: string | undefined, headers?: OutgoingHttpHeaders | OutgoingHttpHeader[] | undefined): ServerResponse<IncomingMessage>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论