Comments (17)
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.
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.
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.
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.
Could you please provide minimal example repo for me to quick jump into it?
from nestjs-session.
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 therequest.user
is not populated
(2h30 to reproduce ! I hope this will help you ! XD)
from nestjs-session.
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.
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.
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.
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.
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.
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.
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.
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.
@robertwestbury reopened your issue
from nestjs-session.
@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.
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.
@Nabellaleen I don't understand how to get graphql to use the session. Any suggestions?
from nestjs-session.
Related Issues (10)
- [QUESTION] Getting "ENOENT: no such file or directory" error with session-file-store HOT 6
- [QUESTION] How to setup this nestjs-session with session-file-store? HOT 1
- [BUG] Not working with @nestjs/graphql HOT 5
- [BUG] misconfigured csrf HOT 1
- [QUESTION] Get Session from Graphql HOT 13
- [QUESTION] How to get session in custom middleware? HOT 7
- How can we access the session middleware? HOT 3
- [BUG] Depracation warnings HOT 1
- [QUESTION] How to use this session in a NestJS GraphQL resolver? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nestjs-session.