Домой United States USA — software How to Manage Sessions in Node.js Using Passport, Redis, and MySQL

How to Manage Sessions in Node.js Using Passport, Redis, and MySQL

133
0
ПОДЕЛИТЬСЯ

Let’s learn about session management and how tools like Passport, Redis, and MySQL can help us manage Node.js sessions.
Join the DZone community and get the full member experience. HTTP and HTTPS are internet protocols that allow data to be sent over the internet by sending a request via a web browser. Because they are stateless, each request sent to the browser is treated independently. This means that the browser cannot remember the source of a request, even if the same user makes it. HTTP sessions solve this problem. This article will look at session management and how tools like Passport, Redis, and MySQL can help us manage Node.js sessions. Let’s dive in. HTTP sessions allow web servers to maintain user identity and store user-specific data across multiple request/response interactions between a client app and a web app. When a client logs into the application, the server generates a SessionID. The session is saved in memory using a single-server, non-replicated persistent storage mechanism. Examples of such mechanisms include JDBC persistence, file system persistence, cookie-based session persistence, and in-memory replication. When the user sends a subsequent request, the sessionID is passed in the request header, and the browser checks if the ID matches any on in the memory storage and grants the user access until the session expires. HTTP sessions store the following data in memory: Redis (Remote Dictionary Server) is a fast, open-source, in-memory key-value data store used as a database, cache, message broker, and queue. Redis has sub-millisecond response times, allowing millions of requests per second for real-time applications in industries such as gaming, ad-tech, finance, healthcare, and IoT. As a result, Redis is now one of the most popular open-source engines, having been named the «Most Loved» database by Stack Overflow five years in a row. Due to its fast performance, Redis is a popular choice for caching, session management, gaming, leaderboards, real-time analytics, geospatial, ride-hailing, chat/messaging, media streaming, and pub/sub-apps. To demonstrate session management in Node.js, we will create simple signup and sign-in application. Users will sign up for and sign in to this application by providing their email address and password. A session is created and saved in the Redis store for future requests when a user signs in. When a user logs out, we will delete their session. Enough talking; let’s get started! This tutorial is a hands-on demonstration. Ensure you have the following installed before getting started: The code for this tutorial is available on my Github repository. Feel to clone and follow along. Let’s start by creating a project folder for the application with the command below: Then, initialize a Node.js application to create a package.json file with the command below: The -y flag in the above command tells npm to use the default configuration. Now create the following folder structure in your project root directory. With our package.json created, let’s install the required package for this project in the next section. We’ll install the following dependencies for our application: Use the command below to install all the required dependencies. Wait for the installation to finish. Once the installation is complete, proceed with setting up the MySQL database in the next section.

Continue reading...