Implementing Session Authentication in Node.js using Express-session

Sandeep Singh
3 min readApr 14, 2023

Session authentication is an essential part of any web application that requires users to sign in. It is the process of verifying a user’s identity and creating a session for that user to access restricted resources on the server. Node.js provides several libraries for session authentication, and in this blog post, we will discuss the most popular one, i.e., Express-session.

Express-session is a middleware for Express.js that provides session management capabilities for web applications. It creates a unique session ID for each user and stores it in a cookie on the client-side. The session ID is used to identify the user and their associated data on the server-side. Here are the steps to implement session authentication using Express-session in Node.js:

Step 1: Install Express-session

The first step is to install Express-session using NPM. Open your terminal and run the following command:

npm install express-session

Step 2: Import Express-session

Next, you need to import Express-session in your Node.js application. Open your server.js file and add the following line at the top of the file:

const session = require('express-session');

Step 3: Configure Express-session

After importing Express-session, you need to configure it to work with your web application. The following code shows an example of how to configure Express-session:

app.use(session({
secret: 'mysecretkey',
resave: false,
saveUninitialized: false
}));

The ‘session’ method takes an object with the following options:

  • secret: A string used to sign the session ID cookie. This value should be unique and kept secret.
  • resave: If set to ‘true’, the session will be saved to the store on every request, even if it hasn’t been modified. Default value is ‘false’.
  • saveUninitialized: If set to ‘true’, a session will be created even if the user hasn’t logged in. Default value is ‘true’.

Step 4: Set up session data

Once Express-session is configured, you can start using it to store session data. The following code shows an example of how to set session data:

app.post('/login', (req, res) => {
// Verify user credentials
if (req.body.username === 'admin' && req.body.password === 'password') {
// Set session data
req.session.loggedin = true;
req.session.username = req.body.username;
// Redirect to dashboard page
res.redirect('/dashboard');
} else {
// Redirect to login page with error message
res.redirect('/login?error=Invalid%20username%20or%20password');
}
});

Here, we set the loggedin and username properties in the session object to true and the user’s username, respectively. You can store any data in the session object that you need to access on subsequent requests.

Step 5: Check session data

Finally, you need to check the session data to restrict access to protected resources. The following code shows an example of how to check session data:

app.get('/dashboard', (req, res) => {
// Check if user is logged in
if (req.session.loggedin) {
// Display dashboard page with user data
res.render('dashboard', { username: req.session.username });
} else {
// Redirect to login page
res.redirect('/login');
}
});

Here, we check if the loggedin property in the session object is true. If it is, we display the dashboard page with the user’s username. Otherwise, we redirect to the login page.

Conclusion

Session authentication is a crucial part of any web application that requires users to sign in. Express-session is a popular middleware for session management in Node.js, and it makes it easy to store and retrieve session data. By following the steps outlined in this blog post, you can implement session authentication in Node js.

--

--