Giter VIP home page Giter VIP logo

Comments (17)

Nabellaleen avatar Nabellaleen commented on June 18, 2024 1

I'll try to find the time to build an example but it's quite a big work, so I admit I'd hope you had an "intuition" about the problem before :p

I'll let you know !

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024 1

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>

from nestjs-session.

clayrisser avatar clayrisser commented on June 18, 2024 1

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.

from nestjs-session.

iamolegga avatar iamolegga commented on June 18, 2024

Could you please provide minimal example repo for me to quick jump into it?

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

Here is an example : https://github.com/Nabellaleen/hello-graphql-session-nestjs !

I've joined a Postman Collection to facilitate the things.

In fact, the problem is not limited to GraphQL and exists also with the REST approach.

To reproduce, you just have to do a login request, then a me request, and see what happens :

  • The login request do the job well, the serializer is called and the user is well stored in the session
  • But for the me request, passport seems to not retrieve the user from the session : the deserializer is not called, and the request.user is not populated

(2h30 to reproduce ! I hope this will help you ! XD)

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

After putting some breakpoints in passport, and in particular here : https://github.com/jaredhanson/passport/blob/master/lib/strategies/session.js#L50

I see that passport base its behaviour on req._passport.session object ... which not exists !

from nestjs-session.

iamolegga avatar iamolegga commented on June 18, 2024

What I can say just now without dive into it, is that you are using passport.session method that could lead to overwriting session value on request. Try to get rid of it, because it is not clean example of usage this library

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

Removing the passport.session is not working (no error, but no success :p)

In the passport documentation about sessions, they say :

 * Note that additional middleware is required to persist login state, so we
 * must use the `connect.session()` middleware _before_ `passport.initialize()`.

But, as @nestjs/passport not implementing the passport.initialize(), I have to do it by hand in main.js, and it is probably added "before" nestjs-session :/

I'll try by moving the passport.initialize into a module

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

But I confirm if I remove nestjs-session module, and directly use, in main.js :

  app.use(session({
    secret: 'a secret',
    name: 'abcd',
    resave: true,
    saveUninitialized: true
  }));
  app.use(passport.initialize());
  app.use(passport.session());

Everything works well

from nestjs-session.

iamolegga avatar iamolegga commented on June 18, 2024

But I confirm if I remove nestjs-session module, and directly use, in main.js :

  app.use(session({
    secret: 'a secret',
    name: 'abcd',
    resave: true,
    saveUninitialized: true
  }));
  app.use(passport.initialize());
  app.use(passport.session());

Everything works well

Is session came here from original express-session module?
Maybe problem with the order of middlewares execution? Ok, I'll dive into it in a few days

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

Yes, it's due to the order of the execution, so I've posted an issue, as mentioned, on nestjs/passport, because it's probably not to you to handle this case

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

Maybe you could help on the nestjs/passport issue nestjs/passport#236

I could make a PR, but I've no idea about how to handle the problem, and with the work you've made on create-nestjs-middleware-module, I'm sure you're able to give me hints ;)

from nestjs-session.

robertt avatar robertt commented on June 18, 2024

Any update on this? My issue was closed as a "duplicate" but I'm not using passport or anything so I don't think it has anything to do with @Nabellaleen's above comment. @iamolegga

from nestjs-session.

iamolegga avatar iamolegga commented on June 18, 2024

@robertwestbury reopened your issue

from nestjs-session.

iamolegga avatar iamolegga commented on June 18, 2024

@Nabellaleen any news? Just got some words from other person who is not using this module, but using express-session directly with nestjs and @nestjs/passport, and he has the same issue too. So, for now we can say that problem is on the side of @nestjs/passport. And this is not related to graphql. So I'm closing this issue, but feel free to keep discussion and inform me please on this

from nestjs-session.

Nabellaleen avatar Nabellaleen commented on June 18, 2024

Yeah, +1, is fully due to @nestjs/passport, but they don't really care about this use case so ...

How I've bypassed the problem is mentioned here : nestjs/passport#236

from nestjs-session.

clayrisser avatar clayrisser commented on June 18, 2024

@Nabellaleen I don't understand how to get graphql to use the session. Any suggestions?

from nestjs-session.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.