문제:

구현을 다했는데 바디에 유저의 아이디와 비밀번호를 넣을 때

{
    "username" : "test1@test.com",
    "password" : "12345678"
}

으로 해야만 성공을 하는 데  "username" 부분을 원하는대로 수정하는 방법을 몰랐다.

 

검색을 해서 알아보니 super() 로 인해 발생한 현상  -> super 부분에서 원하는 대로 수정하여 해결

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({
      usernameField: 'email', // 여기서 'username' 대신 'email'을 사용하도록 설정
      passwordField: 'password', // 필요하다면 여기도 바꾸는 것 가능
    });
  }
  async validate(email: string, password: string): Promise<any> {
    const user = await this.authService.validateUser(new ValidateUserRequestDto({ email, password }));
    if (!user) {
      throw new UnauthorizedException('Cannot find user');
    }
    return user;
  }
}

+ Recent posts