Nest: PassportStrategy: default field value is username and password, how do I set the other fields, example email and password ?

Created on 17 Jun 2020  路  3Comments  路  Source: nestjs/nest

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from '../auth.service';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super();
}

async validate(email: string, password: string): Promise {
const user = await this.authService.validateUser(email, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}

This is my code
It'll return the Unauthorized when I request the interface and add email and password

needs triage question 馃檶

Most helpful comment

Hi @MutelyMilo. This section of the docs shows how you can change the default username/password fields and send other options to a passport strategy:

https://docs.nestjs.com/techniques/authentication#customize-passport
(Customize Passport in the sidebar in case the link does not go to the correct section of the page)

Taking your example, it would look like this:

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({ usernameField: "email" })
  }

  async validate(email: string, password: string) {
    // ...
  }
}

All 3 comments

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

I connot connect the website, maybe because I'm in China ?

Hi @MutelyMilo. This section of the docs shows how you can change the default username/password fields and send other options to a passport strategy:

https://docs.nestjs.com/techniques/authentication#customize-passport
(Customize Passport in the sidebar in case the link does not go to the correct section of the page)

Taking your example, it would look like this:

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({ usernameField: "email" })
  }

  async validate(email: string, password: string) {
    // ...
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hackboy picture hackboy  路  3Comments

tronginc picture tronginc  路  3Comments

menme95 picture menme95  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments

anyx picture anyx  路  3Comments