Maximum Age Setting for Iron Session Cookie

Currently, I am attempting to configure a Next JS application with iron-session and a 'remember me' feature. The objective is for the maxAge of the iron-session cookie to be extended to a week if the user selects the remember me option on the login form, as opposed to the default duration of 24 hours.

Unfortunately, I am facing issues while trying to dynamically set this value. Despite my attempts, such as the code snippet below, I keep encountering an error stating 'rememberMe not defined' when using the ternary operator.

export default withIronSessionApiRoute(
  async function loginRoute(req, res) {
    const { username, password, rememberMe } = req.body;
    await req.session.save();
    return res.status(200).send('Logged in');
  },
  {
    cookieName: 'DEMOAUTH',
    password: 'PmsDH2Hm09rP7XRJkuo7TKDQXtowtBjurW66RUzU',
    ttl: rememberMe ? 60 * 60 * 24 * 7 : 60 * 60 * 24,
  }
);

UPDATE: In response to Jesse's feedback, I made slight adjustments by introducing a constant rememberMe set to true for verification purposes. However, it resulted in the same issue.

export default withIronSessionApiRoute(
  async function loginRoute(req, res) {
    const { username, password } = req.body;
    const rememberMe = true;
    await req.session.save();
    return res.status(200).send('Logged in');
  },
  {
    cookieName: 'DEMOAUTH',
    password: 'PmsDH2Hm09rP7XRJkuo7TKDQXtowtBjurW66RUzU',
    ttl: rememberMe ? 60 * 60 * 24 * 7 : 60 * 60 * 24,
  }
);

Answer №1

rememberMe is located in a different scope than where you are attempting to access it, resulting in it not being defined.

Currently, modifying the iron-session configuration based on the request body is not supported. However, there is a feature request open for this capability.

In this particular situation, one potential workaround could be to create two separate API endpoints with distinct cookie configurations.

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

The accordion seems to be stuck in the open position

Working on a website, I encountered a frustrating bug with an accordion feature. When clicking on the arrow, the accordion opens and closes smoothly. However, when attempting to close it by clicking on the title, the accordion bounces instead of closing p ...

Utilizing jquery and ajax to showcase a series of images prior to uploading them

I have been attempting to create a feature that allows for the preview of multiple images before uploading them to my website. Unfortunately, it's not working as expected and I am unable to identify any errors in the console. Below is the JavaScript c ...

What is the best way to effectively adjust the code structure in a Node.JS project?

[Summarized] Focus on the bold parts. Although I am relatively new to Node.JS, I have been able to successfully build some projects. However, I have come across a burning question that has left me frustrated after searching Google for answers without much ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Upon successful registration, users will be automatically redirected to their profile page

Having trouble with the redirection to the login page from my profile page, which is an HTML file and the main page is the login page. I've tried redirecting to both pages, but it always lands in the catch block whenever a redirect is attempted. angu ...

Compose an email without the need to access the website

Is there a way to send email reminders to users without having to load the website pages? In the reminder section, users can select a date for their reminder and an email will be automatically sent to them on that date without requiring them to access the ...

Upon commencing the project, an error arises with reactstrap when attempting to import the library

Issue with Error console in node_modules/reactstrap/lib/index.js: SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:73:18) at wrapSafe (node:internal/modules/cjs/loader:1176:20) ... Node.js v18. ...

Populate the content within a div element with specified height and width by utilizing JavaScript and jQuery

I need help with filling a fixed height div with long text retrieved via ajax in json format. For example, if I have a div with a height of 500px and width of 300px, with a font size of 16px. Is there a javascript recursive method that can fill the data ...

Why isn't the image utilizing all the available space?

I'm working on creating a simple card to display services, with just an image and some text. However, I'm facing an issue where the image is not taking up the full width and height of the container as expected. Could someone help me figure out w ...

What causes the Invalid Form Body error to appear when using the Discord API?

While developing a Discord bot, I encountered an issue with creating a ping command. The error message received was as follows: (node:37584) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.footer.icon_url: Scheme "flashybot& ...

Nuxt: Delaying Loading of Google Maps in VueJS Until Data is Fully Prepared

Hello, I am currently working on my very first VueJS project and I have successfully implemented the vue2-google-maps. However, I have encountered a problem while trying to connect the map markers to my site's JSON feed through the Wordpress REST API. ...

The Videojs controls are unresponsive to clicks

Having a strange issue with videojs. I've been attempting to load videojs in the same way as outlined in the documentation, using a dynamic video tag. videojs(document.getElementById('myVideo'), { "controls": true, "autoplay": false, "prelo ...

Show segments of video stream on page using node.js

I am currently exploring Node.js and trying to understand how streams work, so please bear with me if I seem a bit unclear. After reading some beginner notes on streams, I decided to test it out. In my project files, specifically in public/images/videos/S ...

When using the next/link redirect feature, it is recommended that the page be set to scroll to the top

I'm working on a Next.js website with links in the footer. When I click on one of these links, the page first redirects to the new page with the current position scrolled down and then scrolls back up. However, I want it to default to the top of the p ...

Attempting to conceal the API, however, the backend is throwing an error

view the error image I am currently in the process of developing an NFT search application that is capable of locating the NFTs associated with a specific wallet address. However, I am faced with the challenge of using Alchemy without exposing the API key ...

Using JQUERY to create a dropdown menu that dynamically changes based on the date field

I am currently working on a JQUERY project and facing a challenging situation. I usually know how to create dropdown menus that depend on one another, but this time I need a dropdown menu that displays age ranges based on the date entered in the birth-date ...

Tips for accessing a variable value within a JavaScript function

I am currently facing an issue where I am unable to retrieve a variable from a JavaScript function and use it outside of the function. While I can successfully output the variable value inside the function, I am struggling to access it elsewhere in my sc ...

Retrieve the page dimensions from a Material UI component `<DataGrid autoPageSize/>`

When utilizing <DataGrid autoPageSize/>, as outlined in the documentation, you can have a Material UI table that adjusts its page size based on the browser height. However, if you are fetching data from the server progressively, it becomes essential ...

Display or conceal the nearest element that was clicked

http://jsfiddle.net/KevinOrin/xycCx/1/ jQuery('.happening-main-text .readmore').click(function() { jQuery('.divmore').toggle('slow'); return false; }); ...

method for sorting labels in Select element in ReactJS

Hey there, I'm facing an issue with the code snippet available here. I would really appreciate it if you could assist me in resolving this problem. This is the code: import React from "react"; import { Select } from "antd" ...