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

264
Visualizações
how to get cookies in next js

i am working with next js and trying to get a cookie value and this is my code:

import cookie from "js-cookie";

export default function Home({ token }) {
  return (
    <div className="flex flex-col items-center bg-reddit-dark py-2 text-white">
      {/* setting cookie */}
      <button
        onClick={() => {
          cookie.set("token", "ABCD");
        }}
      >
        login
      </button>
      {/* removing cookie */}
      <button
        onClick={() => {
          cookie.remove("token");
        }}
      >
        logout
      </button>
      <h1>token: {token}</h1>
    </div>
  );
}

export const getServerSideProps = async ({ req, res }) => {
  const res = await fetch(`http://localhost:5000/api/posts/`);

  const posts = await res.json();

  return {
    props: {
      token: req.cookies.token,
      posts,
    },
  };
};

so i can get and remove a cookie fine but when i try to get the value of the cookie using the getServerSideProps() function it returns undefined and i have cleared cookies from my browser but still did not work i have looked at countless solutions and copied it but it did not work either.

Update i was calling this component in _app.js and now i am calling this in every individual page so is there anything else i can do for this

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

0

According to this answer, you can access to your cookies at 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