Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
cannot get cookies that is set from backend

My frontend is running on localhost:3000 while backend which is on nestjs is running on localhost:3006. When user signs up or logs in then I have set cookie in the following way

const setCookie = context.res.cookie('cookie-data', {
      accessToken: token.accessToken,
      refreshToken: token.refreshToken,
      user: user,
    });

Now, I need this cookie to authenticate each route. I mean if I go to particular page which is a private page then I need to check if those token exist and is valid or not from cookie by calling api for that particular user through the token I get from cookie. If it's valid then allow user to navigate to that page otherwise redirect user to login.

This is how I am trying to get the cookie that's set from backend while user logs in

_app.tsx

function MyApp({ router, pageProps, Component, cookies }) {
  return (
    <>
      <Header />
      {
        Component.auth ?
          <Auth cookies={cookies}>
            <Component key={router.route} {...pageProps} />
          </Auth> :
          <Component key={router.route} {...pageProps} />
      }
    </>
  )
}

MyApp.getInitialProps = async (appContext) => {
  const appProps = await App.getInitialProps(appContext);
  let cookies = {};
  if (process.browser === false) {
    cookies = appContext.ctx.req?.cookies;
  } else {
    cookies = Cookies.get();
  }

  return { ...appProps, cookies }
}

export default MyApp

The problem is appContext.ctx.req?.cookies; returns an empty object. How can I receive cookie that is set from backend(nestjs) ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It should be

cookies = appContext.ctx.req.headers?.cookie;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda