1

How I can pass 2 more fiels as username in this scenario? I want to do login by phone_number and SSA not only with email.

export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authenticationService: AuthenticationService) {
        super({
            usernameField: 'email'
        });
    }
    async validate(email: string, password: string) {
        return this.authenticationService.getAuthenticatedUser(email, password);
    }
}
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
Onivaldo
  • 21
  • 2
  • Does this answer your question? [Using PassportJS, how does one pass additional form fields to the local authentication strategy?](https://stackoverflow.com/questions/11784233/using-passportjs-how-does-one-pass-additional-form-fields-to-the-local-authenti) – Lawrence Cherone Jun 28 '21 at 22:42

2 Answers2

0

You can pass the passReqToCallback option to your super() call in the constructor, this will provide our validate function with the complete req object as the first parameter so we can use it at will like so:

export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authenticationService: AuthenticationService) {
        super({
            usernameField: 'email',
            passReqToCallback: true
        });
    }
    async validate(req: any, email: string, password: string) {
        // Inspect the req object as you wish
        return this.authenticationService.getAuthenticatedUser(req.body.phone_number, req.body.SSA);
    }
}

I took this question as reference, it should work the same for the nestjs/passport library. Using PassportJS, how does one pass additional form fields to the local authentication strategy?

Manuel Duarte
  • 644
  • 4
  • 18
-1

try this.

export class LocalStrategy extends PassportStrategy(Strategy) {
    constructor(private authenticationService: AuthenticationService) {
        super({
            usernameField: 'phone_number'
        });
    }
    async validate(phone_number: string, password: string) {
        return this.authenticationService.getAuthenticatedUser(phone_number, password);
    }
}

I looked for information if new fields could be added, but I did not find anything in the documentation and forums

ChanoHL
  • 1
  • 1