[NestJS] 인증 기능 구현

·2023년 2월 12일
0

NestJS

목록 보기
4/6

모듈 생성

  • nest g module auth
    auth 모듈 생성
  • nest g controller auth --no-spec
    auth 컨트럴로 생성
  • nest g service auth --no-spec
    -auth 서비스 생성

User를 위한 Entity 생성

user.entity.ts

import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  username: string;

  @Column()
  password: string;
}

Repository 생성

user.repository.ts

module에 imports 추가

auth.module.ts

imports: [TypeOrmModule.forFeature([UserRepository])], // 추가
  • 생성된 User Repository를 다른 곳에서 사용하기 위함
  • forFeature : 모듈 내에 UserRepository 등록

Repository Injection

auth.service.ts

constructor(
    @InjectRepository(UserRepository)
    private userRepository: UserRepository,
) {} // 추가
  • UserRepositoryauthSercive 안에서 사용하기 위함

Q. Controller에서 Service 클래스를 DI 받을 때는 별도의 데코레이션 없이 DI를 받았는데, service 계층에서 repository를 DI 받을 때는 왜 @InjectRepository가 필요할까?

profile
개발자가 되는 과정

0개의 댓글