Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static maxAge #659

Closed
jordanpurinton opened this issue Dec 5, 2023 · 1 comment
Closed

Static maxAge #659

jordanpurinton opened this issue Dec 5, 2023 · 1 comment

Comments

@jordanpurinton
Copy link

Each time I update the session I want to maintain the previous value of the Max Age value in the cookie. However, using the options presented in the documentation I can't seem to get this working. Can we looking into adding support for this?

@vvo
Copy link
Owner

vvo commented Jun 14, 2024

Hey there, I just found a solution how to do this.
First, on login, you compute a date in milliseconds you want to use as the date you want the cookie to expire, no matter what. Then you store this value in the session object directly, and you update the config, something like:

function login() {
  const session = await getIronSession<SessionData>(cookies(), sessionOptions);
  const expirationTimestamp = Date.now() + 30 * 24 * 60 * 1000;
  session.isLoggedIn = true;
  session.counter = 0;
  session.expirationTimestamp = expirationTimestamp;

  session.updateConfig({
    ...sessionOptions,
    cookieOptions: {
      ...sessionOptions.cookieOptions,
      maxAge: undefined,
      expires: new Date(session.expirationTimestamp),
    },
  });
  await session.save();
}

Then everytime you call session.save(), make sure to:

function sessionAction() {
  const session = await getIronSession<SessionData>(cookies(), sessionOptions);
  session.counter++;

  session.updateConfig({
    ...sessionOptions,
    cookieOptions: {
      ...sessionOptions.cookieOptions,
      maxAge: undefined,
      expires: new Date(session.expirationTimestamp),
    },
  });
  await session.save();
}

@vvo vvo closed this as completed Jun 14, 2024
Repository owner locked and limited conversation to collaborators Jun 14, 2024
@vvo vvo converted this issue into discussion #767 Jun 14, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants