express session에서 cookie의 만료 시간이 제대로 적용되지 않는 문제가 있었다. 하루의 초 단위인 86400을 넣었는데 하룻동안 유지되지 않고 사라지는 문제가 있었다.
console.log(SESSION_MAX_AGE); // 86400
const sessionMiddleware = session({
store: new RedisStore({ client: redisClient }),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: { maxAge: SESSION_MAX_AGE },
});
일반적으로 쿠키 헤더의 maxAge옵션은 초단위로 설정하지만 express session의 maxAge는 밀리초 단위기 때문에 1000을 더 곱한 값을 넣어줘야한다.
express session 공식 문서
express session에서 maxAge를 넣을 경우 maxAge는 보이지 않고 expire만 생기게 되는데 이는 maxAge옵션을 ms로 계산한 후에 서버 시간을 통해 Expire 날짜를 만들기 때문이다.
interface CookieOptions {
/**
* Specifies the number (in milliseconds) to use when calculating the `Expires Set-Cookie` attribute.
* This is done by taking the current server time and adding `maxAge` milliseconds to the value to calculate an `Expires` datetime. By default, no maximum age is set.
*
* If both `expires` and `maxAge` are set in the options, then the last one defined in the object is what is used.
* `maxAge` should be preferred over `expires`.
*
* @see expires
*/
maxAge?: number | undefined;