代码之家  ›  专栏  ›  技术社区  ›  Abhyudit Jain

警卫在NestJS中不执行Passport Startegy

  •  1
  • Abhyudit Jain  · 技术社区  · 7 年前

    我正在使用NestJS,并且在使用防护验证请求时遇到问题。

    Gist (full code)

    import { PassportStrategy } from '@nestjs/passport';
    import { Injectable, UnauthorizedException, HttpStatus, Logger } from '@nestjs/common';
    import { Strategy } from 'passport-localapikey-update';
    import { size } from 'lodash';
    
    import { AuthService } from './auth.service';
    
    @Injectable()
    export class ApiKeyStrategy extends PassportStrategy(Strategy, 'localapikey') {
        constructor(private readonly authService: AuthService) {
            super();
        }
    
        async validate(token: string) {
            Logger.log('HERE!!!!!!!!!!!!!!', 'ApiKeyStrategy');  // Not printed
            const data = await this.authService.authenticateClient(token);
            if (!size(data)) {
                throw new UnauthorizedException('Unauthorized');
            }
            return data;
        }
    }
    

    这个 @UseGuards(AuthGuard('localapikey')) 不执行并引发401错误。

    没有打印任何日志。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Kim Kern    7 年前

    super

    constructor(private readonly authService: AuthService) {
      super((token, done) => done(null, this.validate(token)));
    }
    

    constructor(private readonly authService: AuthService) {
      super({apiKeyField: 'myapikeyfield'}, (token, done) => done(null, this.validate(token)));
    }
    

    Logger this thread