Implementing a cookie-based session timeout in JavaScript

Session timeouts are an important security measure to protect user data and prevent unauthorized access to a website. In this blog post, we will explore how to implement a cookie-based session timeout in JavaScript, ensuring that users are automatically logged out after a certain period of inactivity.

Why Use Cookies?

Cookies are small pieces of data stored on the user’s computer and can be accessed by the website. They are commonly used to store user-specific information and maintain state between different requests.

Using cookies for session management allows for greater control and flexibility. By setting an expiration time for the cookie, we can automatically log out the user after a predetermined interval of inactivity.

To implement session timeout using cookies, we need to set up the cookie when the user logs in or performs any activity on the website.

function setCookie(name, value, expiration) {
  // Convert expiration time to milliseconds
  const expirationMs = expiration * 60 * 1000;

  // Set the cookie with the specified name, value, and expiration time
  document.cookie = `${name}=${value};expires=${new Date(new Date().getTime() + expirationMs)};path=/`;
}

In the setCookie function, we convert the expiration time from minutes to milliseconds. We set the cookie with the specified name, value, and expiration time using the document.cookie property.

Checking for Session Timeout

To check for session timeout, we need to monitor user activity and reset the expiration time after each action. We can achieve this by utilizing event listeners.

let timeout;

function resetSessionTimeout(expiration) {
  clearTimeout(timeout);
  timeout = setTimeout(logout, expiration * 60 * 1000);
}

function onUserActivity() {
  resetSessionTimeout(15); // Set session timeout to 15 minutes
}

// Attach event listeners to relevant user actions (e.g., mousemove, keydown)
document.addEventListener("mousemove", onUserActivity);
document.addEventListener("keydown", onUserActivity);

In this code snippet, we define a resetSessionTimeout function that resets the timeout whenever there is user activity. This function takes the expiration time as an argument and sets the timeout variable using the setTimeout function. We clear the existing timeout using clearTimeout to ensure that the timeout is reset with each new action.

Logging Out the User

When the session timeout occurs, we need to log out the user and perform any necessary cleanup.

function logout() {
  // Perform any logout-related tasks (e.g., redirect to login page)

  // Clear the session cookie
  document.cookie = "session=;expires=Thu, 01 Jan 1970 00:00:00 UTC;path=/;";
}

The logout function is responsible for performing necessary tasks related to logging out, such as redirecting the user to the login page. Additionally, we clear the session cookie by setting its expiration time to a date in the past.

Conclusion

By implementing a cookie-based session timeout in JavaScript, we can enhance the security of our web applications. By monitoring user activity and setting a cookie with an expiration time, we can automatically log out inactive users and protect their data.

Don’t forget to consider the specific requirements of your application and adjust the session timeout period accordingly. Remember, security is a never-ending process, and session timeouts are just one of many measures to ensure data protection.

#webdevelopment #javascript