nestjsnestjs-passport

Nestjs BasicStrategy doesnt accept empty password


I try to implement basic authentication in nestjs like:

@Injectable()
export class BasicStrategy extends PassportStrategy(Strategy, 'basic') {
    constructor(private configService: ConfigService) {
        super({
            passReqToCallback: true,
        });
    }

    public validate = async (req, username, password): Promise<boolean> => {
        const keys = this.configService.get<string>('SECURE_KEY').split(',');
        if (
            keys[0] === username
        ) {
            return true;
        }
        throw new UnauthorizedException();
    }
}

When I send request with username: ... and password: "", it gives unauthorized error. Is there a solution for it? Any suggestion?enter image description here


Solution

  • passport-http expects the password to be sent. If you want to be able to have it blank, you'll need your own custom strategy