Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[express-session] Can't keep session values with default options. #25

Open
monaka opened this issue Sep 12, 2019 · 7 comments
Open

[express-session] Can't keep session values with default options. #25

monaka opened this issue Sep 12, 2019 · 7 comments

Comments

@monaka
Copy link
Contributor

monaka commented Sep 12, 2019

In @nest-middlewares/express-session, always new session object is created per routing when options was applied.

if (ExpressSessionMiddleware.options) {
expressSession(ExpressSessionMiddleware.options)(req, res, next);

And express-session creates new instance of MemorySession if option.store wasn't specified.

https://github.com/expressjs/session/blob/10607bdb780204b91a8cf90e4ce27726619b8285/index.js#L99-L100

So the app can't keep values in the session.

I suspect the value of expressSession should be cached in the class ExpressSessionMiddleware.

@monaka
Copy link
Contributor Author

monaka commented Sep 12, 2019

Workaround for now: setting sessionStore explicitly. Like

export class ApiModule {
  private sessionStore = new MemoryStore();

  configure(consumer: MiddlewareConsumer) {
    ExpressSessionMiddleware.configure({
      store: this.sessionStore,

@wbhob
Copy link
Owner

wbhob commented Sep 12, 2019

can you open a PR? I didn't know it worked like this. I'd be happy to merge

@ShareQiu1994
Copy link

I had the same problem, I have the same problem. Each session cannot be saved

my code

     ExpressSessionMiddleware.configure({
      secret: 'liubf', 
      name: 'nest',
      resave: false,
      saveUninitialized: false, 
      rolling: true, 
      cookie: {
        maxAge: 60 * 1000 * 30, 
      },
    });

@ShareQiu1994
Copy link

(QJZO(29PN2XU8UG{40)%KU

@wbhob
Copy link
Owner

wbhob commented Feb 3, 2020

I'd be glad to accept a PR for this issue

@vip30
Copy link
Contributor

vip30 commented Jul 15, 2020

export class ExpressSessionMiddleware implements NestMiddleware {

    // DELETE THESE LINES IF MIDDLEWARE DOES NOT TAKE OPTIONS
    public static configure(opts: expressSession.SessionOptions) {
        this.options = opts;
    }

    private static options: expressSession.SessionOptions;
    private handler: Express.RequestHandler;
    
    private getHandler() {
        if (this.handler) return this.handler;
        if (ExpressSessionMiddleware.options) this.handler = expressSession(ExpressSessionMiddleware.options);
        else this.handler = expressSession();
        return this.handler;
    }

    public use(req: any, res: any, next: any) {
        this.getHandler()(req, res, next);
    }
}

Seems modify the function to singleton will work
I am fine to make a pr for this changes

@wbhob
Copy link
Owner

wbhob commented Oct 27, 2023

Fixed in 10.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants