NestJs auth관련 생성, 2024-01-29

sunj - Jan 29 - - Dev Community
nest g module auth
Enter fullscreen mode Exit fullscreen mode
nest g controller auth --no-spec
Enter fullscreen mode Exit fullscreen mode
nest g service auth --no-spec
Enter fullscreen mode Exit fullscreen mode

--no-spec은 테스트 파일을 만들지 않기 위해 붙인다.

그 후 user.entity.ts파일 생성
유저 데이터를 위한 user.entity이다

import { BaseEntity, Column, PrimaryGeneratedColumn } from "typeorm";

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

    @Column()
    email: string;

    @Column()
    passeword: string;
}
Enter fullscreen mode Exit fullscreen mode

@PrimaryGeneratedColumn()은 id는 고유값을 가져야 하기 때문에 지정

user.repository.ts 생성
user.entity를 생성, 수정, 삭제 등의 로직을 처리하기 위해서 repository를 생성

import { EntityRepository, Repository } from "typeorm";
import { User } from "./user.entity";

@EntityRepository(User)
export class UserRepository extends Repository<User>{

}
Enter fullscreen mode Exit fullscreen mode

user.repository를 다른 곳에서도 쓸 수 있게 하기 위해서 auth.module에 추가

import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserRepository } from './user.repository';

@Module({
  imports:[
    TypeOrmModule.forFeature([UserRepository])
  ],
  controllers: [AuthController],
  providers: [AuthService]
})
export class AuthModule {}

Enter fullscreen mode Exit fullscreen mode

user.repository를 auth.service에서 사용하기 위해서 추가

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { UserRepository } from './user.repository';

@Injectable()
export class AuthService {
    constructor(
        @InjectRepository(UserRepository)
        private UserRepository: UserRepository
    ){}
}

Enter fullscreen mode Exit fullscreen mode

참조 : https://www.youtube.com/watch?v=3JminDpCJNE&t=13257s

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player