Carol Skelly
Carol Skelly

Reputation: 362360

Node, Express 3: Track last user visit when req.session loaded from cookieSession

I'm using NodeJS and Express 3. To "permanently" remember the user's login I'm setting a non-expiring cookie like this...

app.configure(function(){
        ..
        app.use(express.cookieSession({cookie:{path:'/',httpOnly:true,maxAge:null},secret:'mysecrettt'}));

});

This works fine, and the req.session is automatically created when the user returns.

However, I'd like to detect the date/time when the user does return the track their last visit. How can I accomplish this? Thanks.

Upvotes: 0

Views: 1682

Answers (1)

Laurent Perrin
Laurent Perrin

Reputation: 14881

Can't you just store the last visit in the session:

app.all('*', function findLastVisit(req, res, next) {
  if (req.session.visited)
    req.lastVisit = req.session.visited;

  req.session.visited = Date.now();

  next();
});

This will retrieve the previous last visit and store the current date (that will be the next last visit). You can decide when you consider that the user starts a new visit (not necessarily on all routes like in my example).

Hope that helps

Upvotes: 4

Related Questions