Hi you can use a function like this ======> // Set the amount of time before the user is logged out (in milliseconds) const LOGOUT_TIME = 300000; // 5 minutes let timer; // Declare the timer variable // Function to reset the timer function resetTimer() { clearTimeout(timer); timer = setTimeout(logout, LOGOUT_TIME); } // Function to logout the user function logout() { // Perform the logout operation console.log("User has been logged out due to inactivity."); } // Add event listeners to monitor user interaction document.addEventListener("mousemove", resetTimer); document.addEventListener("keypress", resetTimer); // Start the timer resetTimer(); So basically the function logs out a user if there in no activity.
This was really useful and straightforward. Thanks
hi, how to logout when closed the broswer. can you please upload a video on that. i am usins redux state management to store token.
Hi you can use a function like this ======>
// Set the amount of time before the user is logged out (in milliseconds)
const LOGOUT_TIME = 300000; // 5 minutes
let timer; // Declare the timer variable
// Function to reset the timer
function resetTimer() {
clearTimeout(timer);
timer = setTimeout(logout, LOGOUT_TIME);
}
// Function to logout the user
function logout() {
// Perform the logout operation
console.log("User has been logged out due to inactivity.");
}
// Add event listeners to monitor user interaction
document.addEventListener("mousemove", resetTimer);
document.addEventListener("keypress", resetTimer);
// Start the timer
resetTimer();
So basically the function logs out a user if there in no activity.
@@demawocodes thanks very useful
its better u dont loggedIn user when he/she already loggedin