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.

Include the passport middlewares initialization in the module

See original GitHub issue

I’m submitting a…


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

With the current version, it seems, to use passport with cookies sessions, we have to declare its middlewares manually in main.js, like :

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

This approach prevents me to use Nest injection, so it prevents me to use my ConfigurationModule & TypeORMModule for example to configure the session store.

Expected behavior

So I would like this nestjs/passport module register passport.initialize() and passport.session() automatically.

I’ve tried, by “monkey-patching” the library to add this configure implementation in PassportModule code :

    configure(consumer) {
        const DEFAULT_ROUTES = [{ path: '*', method: common_1.RequestMethod.ALL }];
        const middlewares = [
            passport.initialize(),
            passport.session(),
        ]
        consumer
            .apply(...middlewares)
            .forRoutes(...DEFAULT_ROUTES)
    }

But maybe it should be “configurable”, maybe it could depend on the session options flag. But I don’t know how to implement such a case in practice. I wonder if we have to implement forRoot / forRootAsync on PassportModule to do it this way ?

Minimal reproduction of the problem with instructions

The following repository provide a good base to reproduce the case : https://github.com/Nabellaleen/hello-graphql-session-nestjs

What is the motivation / use case for changing the behavior?

In practice, this change is motivated by this discussion : https://github.com/iamolegga/nestjs-session/issues/181

Environment


- @nestjs/common: 7.0.5
 
For Tooling issues:
- Node version: v10.15.3
- Platform:  Linux

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
raphaelsoulcommented, Nov 12, 2021

at least we should document it. the current document about @nestjs/passport miss quite a lot of important but very detailed features and considering only stateless backend using jwt

3reactions
Nabellaleencommented, Apr 6, 2020

I don’t talk about “handling express-session” and so to “have additional modules” for each strategy

I talk about handling the core passport feature of “session” This “core” is probably used by more than one strategy And “session” should be easy to do, because so many people are just putting their JWTs in LocalStorage to reinvent the wheel

Read more comments on GitHub >

github_iconTop Results From Across the Web

passport.js passport.initialize() middleware not in use
I have noticed that app.use(app.router); is called after passport initialize but I call: require('./config/routes')(app, passport, ...
Read more >
Using Passport for authentication in Node.js - LogRocket Blog
We are initializing Passport and the session authentication middleware first. Once this is done, we have to set up the local authentication.
Read more >
Node JS with Passport Authentication simplified - Medium
Passport JS — Steps and Syntax · Step 1: Import the libraries into your file · Step 2: Initialize Middleware · Step 3:...
Read more >
Passport - npm
Passport is Express-compatible authentication middleware for Node.js. Passport's sole purpose is to authenticate requests, which it does through ...
Read more >
Complete Tutorial on Node.js Passport [Practical Examples]
Understanding Node.js Passport module in layman's terms ... The express session middleware initializes a session on the server.
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