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