dovahkiin
dovahkiin

Reputation: 718

Executing code when a session ends in sails.js

Using sails.js, is there a way to run a function when a user session expires or is finished? Some configuration to do in config/session.js?

I know exists session.destroy, which you can set a function to execute when the session is destroyed, but I need it to be a global unique function for the application.

The idea would be writing in db table the state of a user as offline, when it's session ends.

Thanks.

Upvotes: 0

Views: 428

Answers (2)

Matt Wielbut
Matt Wielbut

Reputation: 2692

If you're asking if there is a way to see if a user's session has expired -

Yes! It depends on how you're storing the server-side component of the session. Remember, traditional sessions require 2 pieces to work correctly - something on the client side (a cookie for example) and something on the server side to remember the user. In Sails the server-side piece is stored in the data store specified in the adapter portion of the Session Config File. You can query this data-store (even if it's the default Memory Store) and look for all users that have expired sessions.

Going deeper...

If you're asking if there is a specific method that gets called when a user session expires, then no, that's not the way sessions work. Sessions are a "hack" to make HTTP stateful. They aren't an active/live thing in the way that if they die we are notified. A session is just a record (likely a database) with a long code and a date. When the user visits your site, they give you a code from their cookie and you verify against the record in your session database. If the record matches and hasn't expired, HURRAY! you know who they are and they continue with their request. If the record doesn't match or has expired, BOO!, prompt them to log in again.

Really jumping to conclusions now...

I presume from the last sentence that you're looking to try to monitor whether someone is logged in to track "active" users. I would suggest that sessions are a poor metric of that. With sessions I can log in to your site and then leave. Depending on the length of your session expiration (24 hours or 30 days are typical values) I would be shown as logged in for that entire time. Is that a really helpful metric? I'm not using using your site but you're showing me as "logged in". Furthermore I could come back on another device (phone or another browser) and I would be forced to log back in. Now I have 2 or more sessions. Which one is correct?

If you're trying to gauge active usage I would either use Websockets (they would tell you exactly when someone is connected/disconnected to one of your pages - read more here) or just have a "heartbeat" - Each time a user visits one of your pages that visit is recorded as last seen at. This gives you a rough gauge as to who is actively on the site and who hasn't done anything in, say, over an hour.

Upvotes: 1

Ahmed farag mostafa
Ahmed farag mostafa

Reputation: 2964

You can do this by adding policy to all route for example add sessionAuth.js to policy folder :

    module.exports = function(req, res, next) {

  // If you are not using passport then set your own logic
  if (req.session.authenticated) {
    return next();
  }

  // if you are using passport then
  if(req.isAuthenticated()) {
    return next();
  }

    //make your logic if session ends here 
      //** do some thing /


    };

add this lines to config/policies.js :

module.exports.policies = {

    '*': 'sessionAuth'
}

Upvotes: 0

Related Questions