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

284
Visualizações
How set expiration date to an localStoarge item in Javascript?

I am working on making premium access to my website. While doing so I want the login that had set the localStorage item as 1 to expire and get deleted after a month. I searched the web, but got confused as I am still learning JavaScript and jQuery. Can you help me out in setting expiration to the localStorage item? Here's the line of code that is setting the localStorage:

localStorage.setItem('ispremium', '1');

And my website is on Blogger platform, if that matters!

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

0

Normally this would be solved on the server side. The localStorage may be cleared if a user selects "clear browsing history" on some browsers. It may not be available across sessions if the user works with multiple browsers or incognito mode. Other than that someone with a bit of technical knowledge can insert the "ispremium" flag easily into his localStorage to gain access to your premium feature.

If you still want to solve this via client, you could store a timestamp instead of a boolean flag and check if the current time is still in the validity range. LocalStorage itself doesn't let you set an expiration date on entries.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can set the actual value as the first time user joined and the time to expire. And whenever the user opens again the website, you need to check that expiration date. But this is extremely unsecure, you shouldn't do that for sessions. Just to answer to your question, you can do the following:

const NAMESPACE = 'MY_ID';
const TIMESTAMP_MODEL = {
  initial: null,
  expiresOn: null
};
const TIMESTAMP = Date.now();

if (!JSON.parse(localStorage.getItem(NAMSPACE))) {
  // when the user access your website for the first time, set the initial and expiresOn values:

  localStorage.setItem(NAMESPACE, JSON.stringify({
    initial: TIMESTAMP,
    expiresOn: TIMESTAMP + 1000*60*60*24*30 // 1month in ms
  }));
} else {
    // then, when user access the website again, check the expiresOn, it it's value is bigger than current date
  const EXPIRE_DATE = JSON.parse(localStorage.getItem(NAMESPACE)).expiresOn;

  if (Date.now() > EXPIRE_DATE) {
    console.log('session expired');
  } 
}  
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