Bousha
Bousha

Reputation: 11

nestjs/swagger How Do I Change The Default "Username" Field in Basic Auth

  @ApiBasicAuth()
  @UseGuards(LocalAuthGuard)
  @Post('login')
  async login(@Request() req) {
    return this.authService.login(req.user);
  }

When I open up the api docs, the auth for this route shows a "username" and a "password" field. I want to change the username field to "email". Is that possible?

Upvotes: 0

Views: 1866

Answers (2)

Micael Levi
Micael Levi

Reputation: 6685

Show us how you're define the body schema for this endpoint. @nestjs/passport will not define this for you.

You could add the decorator @ApiBody({ type: LoginDto }) in your login method

class LoginDto {
  email: string
  password: string
}

Upvotes: 0

Micael Levi
Micael Levi

Reputation: 6685

Note that this has nothing with @nestjs/swagger. You need to pass configuration to passportjs in your class LocalStrategy provider, like this:

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

As the docs states here: https://docs.nestjs.com/security/authentication#implementing-passport-local

please read the entire Nestjs's docs, it is pretty good :)

Upvotes: 1

Related Questions