새로운 인증 기능 구성하는 모듈 생성

nest g module auth

nest g controller auth —no-spec

nest g service auth —no-spec

모듈부터 생성해야 자동으로 다른 프로바이더 들어감!

다른 repository, entity는 기존 방식과 동일하게 제작

import { DataSource, EntityRepository, Repository } from 'typeorm';
import { User } from './user.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { AuthCredentialDto } from './dto/auth-credential.dto';

@EntityRepository(User)
export class UserReporitory extends Repository<User> {
  constructor(
    @InjectRepository(User)
    private dataSource: DataSource,
  ) {
    super(User, dataSource.manager);
  }

  async createUser(authCredentialDto: AuthCredentialDto): Promise<void> {
    const { username, password } = authCredentialDto;
    const user = this.create({ username, password });
    await this.save(user);
  }
}

repository 와 dto 만들고 기존 방식으로 진행!

유저 데이터 유효성 체크

원하는 데이터 길이 조건 주기

import { IsString, Matches, MaxLength, MinLength } from 'class-validator';

export class AuthCredentialDto {
  @IsString()
  @MinLength(4)
  @MaxLength(20)
  username: string;

  @IsString()
  @MinLength(4)
  @MaxLength(20)
  @Matches(/^[a-zA-Z0-9]*$/, {
    message: 'password only accept eng and number',
  }) // 영어, 숫자만 가능한 유효성 조건 넣어주기
  password: string;
}
@Body(ValidationPipe) authCredentialDto: AuthCredentialDto,

이후 validationPipe로 전체 인자에 pipe 걸어주기

Untitled

위반되는 조건에 의해 메세지 리턴된다.