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

131
Visualizações
Auto refresh token and autologout in vue

I am beginner web developer. I make my first project in Laravel and Vue. I need to make:

  1. Auto-logout after 3 minutes - when user is inactive
  2. Auto ping to my token (domain.com/api/refresh).

I try do it in a function in my main.js:

setTimeout(function() {
  window.location.href = "domain.com/logout";
}, 30000);

for refresh, but it's run only one time - when I page reload.

How can I make it? Please help me :)

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

0

You can use the following events mousemove, mousedown, keypress, and touchdown to determine whether a user is active. You will need to functions to start the timer and reset it. The reset will be used for the aforementioned events.

In your main.js, do this:

const timeoutInMS = 180000; // 3 minutes -> 3 * 60 * 1000
let timeoutId;
  
function handleInactive() {
    // Here you want to logout a user and/or ping your token
}

function startTimer() { 
    // setTimeout returns an ID (can be used to start or clear a timer)
    timeoutId = setTimeout(handleInactive, timeoutInMS);
}

function resetTimer() { 
    clearTimeout(timeoutId);
    startTimer();
}
 
function setupTimers () {
    document.addEventListener("keypress", resetTimer, false);
    document.addEventListener("mousemove", resetTimer, false);
    document.addEventListener("mousedown", resetTimer, false);
    document.addEventListener("touchmove", resetTimer, false);
     
    startTimer();
}

Then call setupTimers on page load.

NOTE: The above code is one way you could detect if a user is inactive.

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