question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Middleware not called while using nestjs-session with @nestjs/graphql

See original GitHub issue

What is the current behavior?

I don’t succeed to make this package work with @nestjs/graphql. When I call a REST route, it’s working well, and the session(req, res, next) middleware (from express-session) is well called. But when I call the GraphQL entrypoint, the middleware is not called at all.

I’ve digged deep in nestjs source-code, but I don’t know really where to look.

Even with a forRoutes: [{ path: '/*', method: RequestMethod.ALL }], this change nothing.

What is the expected behavior?

In practice, the consequences are that no cookie is defined in the GraphQL response, and so, the sessions don’t work.

Please mention other relevant information such as Node.js version and Operating System.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
clayrissercommented, Jun 3, 2020

I’m using redis to manage the session. It’s really odd because the session in my controllers works great. However, with graphql, every request creates a brand new empty session.

Session in Controllers Screenshot from 2020-06-02 21-37-27

Session in Graphql Screenshot from 2020-06-02 21-37-38

Notice that only the first session is used in the controller, but all the rest of the sessions are created new with every graphql request.

1reaction
Nabellaleencommented, Mar 25, 2020

So, I’ve built a simple module to do the init and session, and it works well. So I think the problem is not from your side, but from @nestjs/passport side who should integrate a way to register these middlewares.

import {
    createModule,
    FacadeModuleStaticOptional
  } from 'create-nestjs-middleware-module';
import * as passport from 'passport';

interface Options {
    maxDuration?: number
}

export const PassportSessionModule = createModule<Options>(options => {
    return [
        passport.initialize(),
        passport.session(),
    ]
    // app.use(passport.initialize());
    // app.use(passport.session());
}) as FacadeModuleStaticOptional<Options>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nestjs - how to get session in middleware - Stack Overflow
i tried to add a middleware to the app module, with forRoutes call. but the middleware isn't called with the graphql route /graphql...
Read more >
Setting Up Sessions with NestJS, Passport, and Redis
controller an exception is thrown because passport is not initialized but if I use the LoggedInGuard in my auth.controller it's working. The one ......
Read more >
Field middleware | NestJS - A progressive Node.js framework
Field Middleware lets you run arbitrary code before or after a field is resolved. A field middleware can be used to convert the...
Read more >
Nestjs set cookie - salonelibrousato.it
Judging by parameters, Nest doesn't provide us with an ability to set a cookie in resolvers. Configuring Cookie Authentication Service.
Read more >
Nestjs Log Response Data Object - ADocLib
For example: import moesif from 'moesif-nodejs'; 2. Node. js loggers and start using two of the most popular logging packages for Node, I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found