expressjs / session

Simple session middleware for Express
MIT License
6.26k stars 979 forks source link

req.sessionStore.all with typescript bug? #962

Closed pablojsx closed 1 year ago

pablojsx commented 1 year ago

I'm using express-session with tRPC and in my context:

declare module 'express-session' {
  interface SessionData {
    user: User;
  }
}

I wanted to get all user sessions from the database, I tried " req.sessionStore.all" and also " store.client.db().collection('sessions');" but with both I'm getting the same issue. I get the information but it's not of the type that it should be and it is nested inside another array!.

When I get the information from the callback req.sessionStore.all receives I get this:

Captura de pantalla 2023-11-16 002406

All good but this array isn't a "sessionData[]" array. What I mean is that when you use req.sessionStore.all it will return from the callback a sessions array and that's a "sessionData[]" array that doesn't match the info from the pic above.

I get the information but it is nested!! in the pic below, the session

Captura de pantalla 2023-11-16 003020

You can see here what I mean, the info is nested:

Sin títul2o

If I destructure the session object:

Captura de pantalla 2023-11-16 013807

If I cast the information everything works!

Captura de pantalla 2023-11-16 013917

Captura de pantalla 2023-11-16 014529

dougwilson commented 1 year ago

Hello, this method is implemented by the store module you are using, which may need to fiz their implementation. What store are you using?

pablojsx commented 1 year ago

Hello, this method is implemented by the store module you are using, which may need to fiz their implementation. What store are you using?

Hello Doug!. I'm using the MongoDBStore

dougwilson commented 1 year ago

Thanks! It looks like we have a couple of MongoDB ones listed. Do you happen to know the name of the npm module?

pablojsx commented 1 year ago

Thanks! It looks like we have a couple of MongoDB ones listed. Do you happen to know the name of the npm module?

Yep this is all I have for the server:

Captura de pantalla 2023-11-16 015645

dougwilson commented 1 year ago

Ok, cool. It is connect-mongodb-session. Here is where that module implements the .all method that is return what you are not expecting: https://github.com/mongodb-js/connect-mongodb-session/blob/50c80519f54983c3d0a2f6ee6828bacffa5c58dd/index.js#L152

And here is their bug tracker to report the issue: https://github.com/mongodb-js/connect-mongodb-session/issues

pablojsx commented 1 year ago

In Javascript it works fine, but the problem start when you use Typescript since the types won't match the info!

pablojsx commented 1 year ago

Ok, cool. It is connect-mongodb-session. Here is where that module implements the .all method that is return what you are not expecting: https://github.com/mongodb-js/connect-mongodb-session/blob/50c80519f54983c3d0a2f6ee6828bacffa5c58dd/index.js#L152

And here is their bug tracker to report the issue: https://github.com/mongodb-js/connect-mongodb-session/issues

Thanks for your help Doug, I'll report the bug now