Deepak
Deepak

Reputation: 1592

How can I clear express session for all users without restarting the application?

I was developing REST services using nodejs express framework and keeping the user specific data in the session with user's id as key to identify user's data.

Here Is the code I am using to store user specific data:

let session = require("express-session");
let FileStore = require("session-file-store")(session);

//For setting up the data into session
session[userId]={name:xyz, age:22}

//For getting the data
session[userId]

I have also configured session with expiry time, which must clear out all the session value.

  webserver.use(session({
        cookie: {
            maxAge: 60 * 1000 // 24 hours
            // maxAge: 24 * 60 * 60 * 1000 // 24 hours
        },
        key: "sid",
        resave: false,
        saveUninitialized: false,
        secret: "keyboard cat",
        store: new FileStore(storageOptions),
    }));`

but the session values seems persisted even after expiry time exceeded. although these are rest services and no cookies are involved in this, I am using user's id instead.

As per my understanding this session should be clear out after expiry time exceeded but it's not.

Question 1 : How can I clear complete session values anytime without restarting the server ? Question 2 : Why expiry time is not working as per above code ?

Am I doing something wrong ?

Please help

Thanks in advance

Upvotes: 3

Views: 906

Answers (1)

Sparw
Sparw

Reputation: 2743

I think cookie.maxAge is used to define inactivity time. So the session will be detroyed after 24 hours of inactivity.

If you want to detroy user session, you can use somethink like this :

req.session.destroy(function (err) {
  if (err) {
    // Handle error
  }
  else {
    // Session destroyed !
  }
});

Hope it helps !

Upvotes: 1

Related Questions