I'm using express cookie-session: http://expressjs.com/en/resources/middleware/cookie-session.html
I can not find the answer to this and based on some logging there is no way to see the time elapsed. So I'm trying to find a way to check this and see if it has expired and log the user out and destroy the session.
In the isAuthenticated I'd like to see if there is a way to see if the session is expired. My hope was passport method isAuthenticated() would handle all that but it looks like this just checks if there is a user on req.user.
How would I find when the cookie has expired so I can log the user out ?
app.use(
cookieSession({
name: "authSession",
expires: new Date(Date.now() + 1 * 60 * 1000),
keys: [process.env.COOKIE_KEY],
})
);
app.use(passport.initialize());
app.use(
passport.session({
saveUninitialized: true,
resave: false,
secret: process.env.SESSION_SECRET,
})
);
app.get("*", isAuthenticated, (req, res) => {
res.sendFile(indexPath);
});
I have a middleware checking if authenticated.
module.exports.isAuthenticated = (req, res, next) => {
const expired = req.sessionOptions.expires;
const current = new Date(Date.now());
console.log("expired = ", expired, " current = ", current);
if (req.isAuthenticated()) {
console.log(
"req sessionn = ",
req.sessionOptions.maxAge,
);
next();
} else {
res.render(process.env.AUTH_LOGIN_PAGE || "login");
}
};