Trouble updating Express-Session cookie

Hello, I have been working with express.js and have encountered an issue with express-sessions. Here is how my express session is configured in index.js:

app.use(
  session({
  secret: 'idegas',
  resave: false,
  saveUninitialized: false,
  cookie: {
    maxAge: null,
    user_identification: null,
    authorized: false,
  }
}))  

When a user is on the login page and successfully logs in, I want to assign them a session. I achieve this in login.js:

req.session.cookie.expires = new Date(Date.now() + '7200000')
req.session.cookie.user_identification = id
req.session.cookie.authorized = true

After the user is redirected to the dashboard, I check the user's session in the console and see the following output:

Session {
  cookie: {
    path: '/',
    _expires: null,
    originalMaxAge: null,
    httpOnly: true,
    user_identification: null,
    authorized: false
  }
}

It appears that nothing has changed. I'm puzzled as to why this is not working. Any suggestions? Thank you!

Answer №1

req.session.cookie

Every session is accompanied by a unique cookie object. This feature allows you to customize the session cookie for each visitor. For instance, you can set req.session.cookie.expires to false to limit the cookie's lifespan to the duration of the user-agent.

This attribute is specifically designed to modify the behavior of the session cookie.

It is not intended for adding or modifying any other cookies.

For managing other cookies, consider using res.cookie

Answer №2

req.session.cookie is responsible for determining the expiry date of the server-side session, not the sessionId cookie stored in the user's browser.

To make a session expire immediately, you can set

req.session.cookie.expires = false
(which will essentially create a session cookie that is deleted when the server restarts). If you want to specify a particular expiry date, it is recommended to use the req.session.cookie.maxAge property instead of directly setting the expires property.

If you wish for the expiry date to apply to the sessionId cookie stored in the user's browser, you must include the rolling: true option in the session middleware.

Please Note:

  • req.session.cookie should not be used as a cookie to store data; data should be stored within the session itself (e.g.
    req.session.user_identification = id
    )
  • If both expires and maxAge are specified in the options (or as properties), the value defined last in the object will take precedence.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason for Chrome download freezing while serving a PDF from Google Cloud Storage through an Express App in Firebase?

I've developed an express application that serves PDF files from Google cloud storage to clients. When accessing the file endpoint, downloads sometimes stall in Chrome at around 1000KB to 1300KB. However, Firefox, Safari, and Postman usually have no i ...

The functionality of Vue.js checkboxes is not compatible with a model that utilizes Laravel SparkForm

I've configured the checkboxes as shown below: <label v-for="service in team.services"> <input type="checkbox" v-model="form.services" :id="service.name" :value="service.id"/> </label> Although they are displayed correctly, the ...

Splitting arrays in JavaScript

Hi, I have a quick question that I need help with. There's a JavaScript array that I'm working with which looks like this: var myArray = [1, 3, 4, 5, 6, 7]; Now, I am looking to transform this array into the following format: var newArra ...

Struggling to find the right look for identical items

I'm currently working on a project where I need to identify and hide duplicate values in a table. The goal is to only display unique values in the first column and hide any duplicates from view. However, I'm running into an issue when trying to h ...

Firefox encountering difficulty opening subdomains

Currently, I am running a nodejs app with express as my backend on localhost. Within this setup, I have subdomains connected to it such as user1.localhost. While Chrome is able to access these subdomains without any issues, Firefox is displaying a Server N ...

How are Deferred objects resolving differently with multiple Ajax calls being made repeatedly?

I am wondering if there is a more optimized way to handle my ajax requests using deferred. I need to trigger the ajax request multiple times during a user's page view, and I want to ensure that the deferred is resolved appropriately. Can I assign the ...

Designing Object-Oriented JavaScript

Currently, I am in the process of developing a sophisticated JavaScript application. Utilizing an object-oriented design approach, I have organized my code into various files to enhance maintainability. In order to create my application, what is the best ...

Prevent postback when clicking on an image button in ASP

I have a project in ASP where I have an image button that allows users to browse an image and display it. I am utilizing a script for this project. Here is the code I am using: ASPX: <asp:Panel ID="stage" runat="server" cssClass="containment-wrapper" ...

Filtering rows of an HTML table that contain links in the `<href>` column data in real time

When using HTML and JavaScript, I have found a solution that works well for many of the columns I am working with. You can see the details on how to dynamically filter rows of an HTML table using JavaScript here. var filters=['hide_broj_pu',&apo ...

Encountering difficulties resolving the dependency tree while trying to install @react-navigation/[email protected]

After creating a new project called MyProject using react-native init MyProject, I opened it in VSCode and the first task was to install navigation. Initially, when running npm install @react-navigation/native @react-navigation/stack, it threw an error. S ...

Resetting the state back to its initial value - which one to use: useState or useReduce?

In order to enhance the functionality of a third-party Authentication service Auth0's useAuth0 hook, I have developed a custom hook called useAuth. This custom hook is responsible for managing local variables that store essential user information like ...

JavaScript For/in loop malfunctioning - Issue with undefined object property bug

Currently, I am tackling a basic For/in loop exercise as part of a course curriculum I am enrolled in. The exercise involves working with a simple object containing 3 properties and creating a function that takes two parameters - the object name and the it ...

The array remains empty despite attempts to push elements into it

How can I ensure that the elements pushed into the "comments" array are visible outside of the initial foreach loop in this code snippet? const Post = require('../models/post'); const Comment = require('../models/comment'); getpost: a ...

Concluding Engagement After Announcing a Victor in a Virtual Competition

I have developed a straightforward JavaScript tic-tac-toe game that is functioning smoothly at the moment. The game involves an array that stores X's and O's, prevents the same spot from being selected twice, and determines the computer's mo ...

AJAX Object Creation: Only Visible After Manual Refresh

Currently, I am in the process of building a basic to-do app that includes multiple different lists, each of which contains a variety of items. My main objective is to integrate AJAX functionality into the creation and display of lists on the lists#index p ...

When it comes to HTML and Javascript drawing, getting the positioning just right can be a challenge

I'm currently developing a control website for a drawing robot as part of a school project. However, I've been facing some challenges with the functionality of the drawing feature. Though I admit that the site lacks attention to detail, my main ...

Design your very own personalized Show Layout component

Currently, I'm in the process of creating a unique layout component to enhance the design of my Show page. I've encountered some inconsistencies with functionality, and my solution involves utilizing the Material-UI <Grid> component. While ...

Looking to retrieve data using Cheerio? If you're finding that the data appears empty in the page source but is visible when inspecting the elements, here's how you can go

I am encountering difficulties while trying to scrape data from another website. In my case, I notice that the data appears empty when viewing the page source, but it is visible when inspecting the elements. If you're confused, please refer to the fol ...

Express Route returning empty after AJAX request for Image File

While there are a few examples floating around the internet, I encountered a specific issue with my application. I have an input field that allows users to upload images. These images are then sent to a route using an AJAX POST request, but I am running in ...

The failover process for PayPal involves seamless transitions to backup systems in the

I am currently assisting a client with a unique architecture setup: Back End Running on Java Server Front End Powered by Proxy (Node JS + Express application) For security reasons, all requests made to the server must pass through the Proxy. We ar ...