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

[QUESTION] How to get session in custom middleware? #276

Closed
vit100 opened this issue Sep 9, 2020 · 7 comments
Closed

[QUESTION] How to get session in custom middleware? #276

vit100 opened this issue Sep 9, 2020 · 7 comments
Assignees
Labels
question Further information is requested

Comments

@vit100
Copy link

vit100 commented Sep 9, 2020

I have my own middlewares and need to get session from there, from req.session, but it is undefined.

But it is available in nest controllers via @Session() session or @Req() req.session...

How to get session in middleware?

@vit100 vit100 added the question Further information is requested label Sep 9, 2020
@iamolegga
Copy link
Owner

you have to check the order of middlewares. if your custom middleware goes before session middleware, how can it get session object, if it's not created yet. Anyway nestjs doesn't guarantee constant order of middlewares of different modules, so maybe nestjs-configure-after could be useful for you

@vit100
Copy link
Author

vit100 commented Sep 10, 2020 via email

@iamolegga
Copy link
Owner

You can create your module with applying middleware like here:
https://docs.nestjs.com/middleware#applying-middleware

After that create wrapper module for session that imports and initiates session module from library

add to both modules decorators like here: https://github.com/iamolegga/nestjs-configure-after#example

if this still not helps you can create tiny repo with minimal example of that what I've described above, I'll look at it

@vit100
Copy link
Author

vit100 commented Sep 10, 2020

Yeh, this is what I end up doing in my test - wrapper module just to apply After() in all modules. This way it works..

What bothers me is extra module doing nothing except importing Nestjs-Session, plus extra package (nest-configure-after) with After() everywhere....

Kind of too much comparing to simple way in main.ts ...app.use(session())..

Not sure if all this hassle worth it.. Kind of "nest way", but more code, more chances for bugs, etc...

@iamolegga
Copy link
Owner

That's up to you, if you need to get configuration for session from other configuration module/service then that's the case for such setting. If you don't need that then app.use(session()) is better way, it's simpler and less code

@iamolegga iamolegga changed the title [QUESTION] If this package used, then session is not available in custom middleware. [QUESTION] How to get session in custom middleware? Sep 11, 2020
@iamolegga iamolegga pinned this issue Sep 11, 2020
@Strnadj
Copy link

Strnadj commented Oct 4, 2021

I've just spent few hours figuring this out, unfortunately nestjs-session create their own custom module with middleware, I was not able to make things work. Solution which work for me, was to create own custom module with empty After:

@Module()
@After()
class SessionModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    const middleware = expressSession({
        // Session options
    });

    consumer.apply(middleware).forRoutes("*");
  }
}

@Module()
@After(SessionFacadeModule)
export default class UsersModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AuthMiddleware).forRoutes("*"); // This middleware use req.session object
  }

@hbriese
Copy link

hbriese commented Aug 2, 2022

I found the cleanest way to be just to wrap the session() functional middleware as a NestMiddleware class - in order to get the DI

@Injectable()
export class SessionMiddleware implements NestMiddleware {
  private sessionHandler: RequestHandler;

  constructor(/* Dependencies may be injected here */) {
    this.sessionHandler = session({
      ...
    });
  }

  use(req: Request, res: Response, next: NextFunction) {
    this.sessionHandler(req, res, next);
  }
}

Then inject it prior to my auth middleware

@Module({})
export class AuthModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(SessionMiddleware, AuthMiddleware).forRoutes('*');
  }
}

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

No branches or pull requests

4 participants