Next.js Discord

Discord Forum

How do I get import { cookies } from 'next/headers' to identify changes in cookies W.OUT hard-reload

Answered
Bighead carp posted this in #help-forum
Open in Discord
Bighead carpOP
Hi,

I'm trying to add some security to our blog by checking for the JWT cookie before allowing users to "Load More" blog articles and "Search" because we're using PlanetScale and they bill by the amount of row reads. This feature will prevent the risk of bots being able to spam these buttons and drive up our server costs.

This is what the Blog page file looks like:
import { cookies } from 'next/headers';

export default function Blog() {
  // State Variables
  const cookieStore = cookies();
  const hasCookie = cookieStore.has('jwt');

  return (
    <div className={styles.background}>
      {/* Content Area */}
      <ContentArea hasCookie={hasCookie} />
    </div>
  );
}


When users logout the JWT is overwritten with one that instantly expires.

PROBLEM: Seems like this cookies function requires a hard reload to notice the change.

QUESTION: Is there anyway I can get this to work without a hard reload?
Answered by riský
hmm you could try with router.refresh, but that might be wacky
View full answer

11 Replies

just checking if they have the cookie is bad, because that will allow for fake data...
Bighead carpOP
?
I guess I have to do a hard reload on login
yeah hard reload is basically the only way with login because you otherwise can have weird state occuring...
Bighead carpOP
Damn, we have a radio player that plays music made in our software. Was REALLY hoping we didn't have to interupt that :/
hmm you could try with router.refresh, but that might be wacky
Answer
Bighead carpOP
REFRESH() works!
thank you 🙂
Soooooo happy I didn't go down the path of installing another third-party lib or commiting to hard reload 🙂
i rember it said that it updates but keeps state, and now i see it does indeed keep more state