Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
Token de actualización automática y cierre de sesión automático en vue

Soy desarrollador web principiante. Hago mi primer proyecto en Laravel y Vue. Necesito hacer:

  1. Cierre de sesión automático después de 3 minutos: cuando el usuario está inactivo
  2. Hacer ping automático a mi token (domain.com/api/refresh).

Intento hacerlo en una función en mi main.js:

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

para actualizar, pero se ejecuta solo una vez, cuando recargo la página.

¿Cómo puedo hacerlo? Por favor, ayúdame :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar los siguientes eventos mousemove , mousedown , keypress y touchdown para determinar si un usuario está activo. Necesitará funciones para iniciar el temporizador y restablecerlo. El reinicio se utilizará para los eventos antes mencionados.

En su main.js, haga esto:

 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(); }

Luego llame a setupTimers en la carga de la página.

NOTA: El código anterior es una forma de detectar si un usuario está inactivo.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!