Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Iron-session set cookie maxAge

I'm trying to set up a Next JS app with iron-session and a 'remember me' function, whereby if the user ticks the remember me box on the login form then the maxAge of the iron-session cookie is set to a week rather than my default value of 24 hours.

However, I can't work out a way to set the value programatically. I've tried the following but I get 'rememberMe not defined' on the ternary.

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,
  }
);

EDIT following Jesse's comment - I tweaked slightly, adding a const rememberMe = true to check the value was definitely being set and I get the same behaviour

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,
  }
);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

rememberMe is defined outside of the scope in which you are trying to use it. That's why it's not defined.

As far as I know, it's not possible to change the iron-session configuration based on the request body as of now, but here is an issue requesting that feature.

Maybe for this specific case what you can do is create two API endpoints with different cookie configurations.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!