Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJS | Passport: TypeError: Cannot read properties of undefined (reading 'logIn')

Situation:

  • Developing api in nest & grapqhql
  • Worked on one laptop, everything was working well
  • Then cloned my repo on other laptops, installed dependencies, created a new local database.
  • App is being built with no errors
  • When following localhost:4000 in browser to open graphql playground I'm receiving 500 error end next message:
    ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'logIn')
    TypeError: Cannot read properties of undefined (reading 'logIn')
        at authenticate (/home/gleb/Projects/artwine-api/node_modules/passport/lib/middleware/authenticate.js:96:21)
        at /home/gleb/Projects/artwine-api/node_modules/@nestjs/passport/dist/auth.guard.js:91:3
        at new Promise (<anonymous>)
        at /home/gleb/Projects/artwine-api/node_modules/@nestjs/passport/dist/auth.guard.js:83:83
        at JWTAccessAuthGuard.<anonymous> (/home/gleb/Projects/artwine-api/node_modules/@nestjs/passport/dist/auth.guard.js:49:36)
        at Generator.next (<anonymous>)
        at fulfilled (/home/gleb/Projects/artwine-api/node_modules/@nestjs/passport/dist/auth.guard.js:17:58)
        at processTicksAndRejections (node:internal/process/task_queues:96:5)
    

Code of a passport lib function where the error is caught:

return function authenticate(req, res, next) {
    req.login =
    req.logIn = req.logIn || IncomingMessageExt.logIn;
    req.logout =
    req.logOut = req.logOut || IncomingMessageExt.logOut;
    req.isAuthenticated = req.isAuthenticated || IncomingMessageExt.isAuthenticated;
    req.isUnauthenticated = req.isUnauthenticated || IncomingMessageExt.isUnauthenticated;
    
    req._sessionManager = passport._sm;
..............

Link to the repo: https://github.com/Gleb-Gaiduk/artwine-api Any ideas on what could go wrong after cloning the working repository?

like image 322
Gleb Gaiduk Avatar asked Dec 02 '25 12:12

Gleb Gaiduk


2 Answers

You need to transform the ExecutionContext from Graphql to one Nestjs/Passport can read: https://docs.nestjs.com/graphql/other-features#execution-context

import { ExecutionContext, Injectable } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class MySuperGuard extends AuthGuard('jwt') {
  getRequest(context: ExecutionContext) {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req;
  }
}
like image 153
Stevanicus Avatar answered Dec 09 '25 20:12

Stevanicus


try adding this into "subscriptions" section of GraphQL module initialization:

'subscriptions-transport-ws': {
    onConnect: (headersRaw: Record<string, unknown>) => {
        // Lowercase each header key
        const headers = Object.keys(headersRaw).reduce((dest, key) => {
            dest[key.toLowerCase()] = headersRaw[key];
            return dest;
        }, {});
        return {
            req: {
                headers: headers,
            },
        };
    },
},

I have no idea why it is not documented, but it worked for me.

like image 30
Mike Avatar answered Dec 09 '25 19:12

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!