-
Notifications
You must be signed in to change notification settings - Fork 14
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
Comments
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 |
Yeh, I realize that middleware order is important -)).
I am asking because I didn't find a way how to force my middleware to run after nestjs-session module. I expected that in my middleware sessione will be created already, but turned out it is not as my middleware is invoked first.
I import SessionModule and register my ProxyMiddleware in main app.module.ts like this.
@module({ imports: [SessionModule.forRoot(...),..]}) class AppModule implements NestModule { configure(consumer: MiddlewareConsumer): void { consumer.apply(ProxyMiddleware).forRoutes('myPath'); }}
But I will look at nestjs-configure-after module.
Thanks
On Thursday, September 10, 2020, 03:53:34 a.m. EDT, iamolegga <[email protected]> wrote:
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
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
You can create your module with applying middleware like here: 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 |
Yeh, this is what I end up doing in my test - wrapper module just to apply What bothers me is extra module doing nothing except importing Nestjs-Session, plus extra package (nest-configure-after) with Kind of too much comparing to simple way in main.ts Not sure if all this hassle worth it.. Kind of "nest way", but more code, more chances for bugs, etc... |
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 |
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
|
I found the cleanest way to be just to wrap the @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('*');
}
} |
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?
The text was updated successfully, but these errors were encountered: