-
Notifications
You must be signed in to change notification settings - Fork 1
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
[BE] 회원가입 API 구현 #29
[BE] 회원가입 API 구현 #29
Changes from 9 commits
959e10d
8fb0b1f
d169098
643ad3c
a71bf2c
92c7549
bf295a6
226ce86
eee8101
e817f4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { UserRepository } from './user.repository'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: UserRepository, | ||
useValue: { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Controller, Post, Body, ValidationPipe } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟢 이런 네이밍 규칙도 나중에 이야기해보면 좋겠네요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
어라? 저도 진님이 하신게 기억나서 고쳐논건데 왜 다시 되돌아 왔는지 모르겠네요 ㅎㅎ |
||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@Post('/signup') | ||
signUp(@Body(ValidationPipe) authCredentialsDto: AuthCredentialsDto) { | ||
return this.authService.signUp(authCredentialsDto); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { User } from './user.entity'; | ||
import { UserRepository } from './user.repository'; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([User])], | ||
controllers: [AuthController], | ||
providers: [AuthService, UserRepository], | ||
}) | ||
export class AuthModule {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
import { UserRepository } from './user.repository'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
providers: [ | ||
AuthService, | ||
{ | ||
provide: UserRepository, | ||
useValue: { | ||
find: jest.fn(), | ||
findOne: jest.fn(), | ||
save: jest.fn(), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { UserRepository } from './user.repository'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
@InjectRepository(UserRepository) | ||
private userRepository: UserRepository, | ||
) {} | ||
|
||
async signUp(authCredentialsDto: AuthCredentialsDto) { | ||
return this.userRepository.registerUser(authCredentialsDto); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { IsString, Matches, MaxLength, MinLength } from 'class-validator'; | ||
|
||
export class AuthCredentialsDto { | ||
@IsString() | ||
@MinLength(4) | ||
@MaxLength(20) | ||
email: string; | ||
|
||
@IsString() | ||
@MinLength(4) | ||
@MaxLength(20) | ||
@Matches(/^[a-zA-Z0-9]*$/) | ||
password: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity() | ||
export class User extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
email: string; | ||
|
||
@Column() | ||
password: string; | ||
|
||
@Column({ default: false }) | ||
tutorial: boolean; | ||
|
||
@Column({ default: -1 }) | ||
kakaoId: number; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectDataSource } from '@nestjs/typeorm'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
import * as bcrypt from 'bcrypt'; | ||
import { User } from './user.entity'; | ||
import { AuthCredentialsDto } from './dto/authCredentials.dto'; | ||
|
||
@Injectable() | ||
export class UserRepository extends Repository<User> { | ||
constructor(@InjectDataSource() dataSource: DataSource) { | ||
super(User, dataSource.createEntityManager()); | ||
} | ||
|
||
async registerUser(authCredentialsDto: AuthCredentialsDto) { | ||
const { email, password } = authCredentialsDto; | ||
const salt: string = await bcrypt.genSalt(); | ||
const hashedPassword: string = await bcrypt.hash(password, salt); | ||
const user = this.create({ email, password: hashedPassword }); | ||
await this.save(user); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟢 auth.module에서 user 엔티티로 등록되어있는 것 같은데, 혹시 여기서 한번 더 등록해주어야 할 필요가 있는 건가용...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
해당 부분에서 등록하는 부분과 auth.module에서 등록하는 부분이 다른 걸로 알고 있습니다.
TypeOrmModule의 옵션인 entities에 포함 해주면 엔티티에 맞춰서 자동으로 데이터 테이블을 생성해 준다고 알고 있습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
그렇군요!! 감사합니당