NestJS | 护照:类型错误:无法读取未定义的属性(读取“登录”)

Gle*_*duk 18 nest passport.js

情况:

  • 在 Nest 和 grapqhql 中开发 api
  • 在一台笔记本电脑上工作,一切正常
  • 然后在其他笔记本电脑上克隆我的存储库,安装依赖项,创建一个新的本地数据库。
  • 应用程序正在构建,没有错误
  • 当在浏览器中关注 localhost:4000 打开 graphql Playground 时,我收到 500 错误结束下一条消息:
    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)
    
    Run Code Online (Sandbox Code Playgroud)

捕获错误的护照库函数的代码:

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;
..............
Run Code Online (Sandbox Code Playgroud)

存储库链接: https: //github.com/Gleb-Gaiduk/artwine-api 关于克隆工作存储库后可能出现什么问题有什么想法吗?

Ste*_*cus 19

您需要将 ExecutionContext 从 Graphql 转换为一个 Nestjs/Passport 可以读取的内容: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;
  }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*ike 7

尝试将其添加到 GraphQL 模块初始化的“订阅”部分:

'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,
            },
        };
    },
},
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它没有记录,但它对我有用。


Erv*_* H. 5

您应该查看您正在使用的守卫和策略,并应该从那里处理错误,例如:

@Injectable()
export class PassportLocalGuard extends AuthGuard('local') {
protected readonly logger = new Logger(PassportLocalGuard.name);

canActivate(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const { req } = ctx.getContext();

return super.canActivate(new ExecutionContextHost([req]));
   }

handleRequest(err: any, user: any) {
  if (err) {
   this.logger.error(`Auth Error! ${err.message}`);
   throw err;
   }

  if (!user) {
   this.logger.error('Auth Error! User not found');
   throw new AuthenticationError('Auth Error! User not found');
   }

  return user;
 }
}
Run Code Online (Sandbox Code Playgroud)