Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JWT access_token 생성 #2

Open
rnin9 opened this issue Jul 21, 2021 · 0 comments
Open

JWT access_token 생성 #2

rnin9 opened this issue Jul 21, 2021 · 0 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@rnin9
Copy link
Collaborator

rnin9 commented Jul 21, 2021

local.strategy.ts에서 작성한 validate를 LoginUserDto로 설정하였더니 access_token을 생성하지 못했습니다. @UseGuards(LocalAuthGuard)를 통과하지 못함.

**validate 메서드가 로그인 api 실행 이전에 먼저 실행되어 해당 유저의 상태를 검사해주는 역할을 해주는데,
주의할 점은 기본적으로 username 과 password 필드로 설정이 되어있기 때문에 username 이 아닌 email 필드로 검사하고 싶다면 contructor에 위와 같이 usernameField: 'email' 로 해줘야 한다는 것이다.**

따라서 아래와 같이 코드를 작성하여 ID로 체크하는 방식을 구현함.

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({
      usernameField: 'ID', //검사할 필드는 ID
      passwordField: 'password',
      passReqToCallback: false,
    });
  }

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

DTO를 사용하여 값을 확인하는 경우, @Request가 아닌 @Body decorator로 확인할 수 있다.
참고자료 [NestJS / JWT / Passport] 로그인 1. LocalStrategy

@rnin9 rnin9 added bug Something isn't working documentation Improvements or additions to documentation labels Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant