How To Make A Javascript Login Page With Cookies

How To Articles

Today, I want to share with you my personal experience of creating a JavaScript login page with cookies. As a web developer, I have always been fascinated by the power of cookies in managing user sessions. Cookies provide a way to store information on the user’s computer, allowing us to create personalized and interactive web applications. So, let’s dive deep into how I created my own JavaScript login page with cookies!

Getting Started

First, I created a basic HTML structure for my login page. It consisted of a form with fields for username and password, along with a submit button. To make it more visually appealing, I added some CSS styles to make the form look nice and user-friendly.

Next, I wrote the JavaScript code for handling the form submission. When the user clicks on the submit button, the JavaScript code is triggered. In this code, I first retrieve the values entered by the user in the username and password fields.

const username = document.getElementById('username').value;
const password = document.getElementById('password').value;

Then, I performed some basic validation to ensure that both fields are filled in before proceeding further. This helps to prevent any empty submissions from being processed.

if (username === '' || password === '') {
alert('Please enter both username and password');
return false;

Working with Cookies

Now comes the exciting part – working with cookies! In order to store the user’s login information, I created a cookie using the document.cookie property. This property allows us to read, write, and delete cookies. In my case, I wanted to create a login session cookie that would be valid for a certain period of time.

const expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + 7); // Cookie will expire in 7 days

document.cookie = `username=${username}; expires=${expirationDate.toUTCString()}; path=/`;

I set the expiration date of the cookie to 7 days from the current date using the setDate() method. This means that the user will remain logged in for a week unless they manually log out or clear their cookies.

Whenever the user visits the login page, I added code to check if the cookie exists. If it does, I automatically fill in the username field with the stored value, making it easier for returning users to log in.

const storedUsername = getCookie('username');

if (storedUsername !== '') {
document.getElementById('username').value = storedUsername;

Here, the getCookie() function is a helper function that I created to retrieve the value of a cookie by its name. If the cookie does not exist, the function returns an empty string.


Creating a JavaScript login page with cookies can be a powerful way to enhance user experience and provide personalized interactions. By storing user login information in cookies, we can create seamless login experiences and improve the security of our web applications.

In this article, I shared my personal journey of creating a JavaScript login page with cookies. I hope you found it informative and inspiring. If you’re interested in exploring this topic further, I encourage you to experiment with different cookie settings and explore additional security measures to protect user information.

Now it’s your turn to create your own JavaScript login page with cookies. Happy coding!