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

[임동민] 휴대폰 인증 API #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
9,264 changes: 9,264 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 2 additions & 0 deletions template/source/typeorm/app.module.ts → src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import { addTransactionalDataSource } from 'typeorm-transactional';
import { DataSource } from 'typeorm';
import { VerificationModule } from './verification/veficitaion.module';

@Module({
imports: [
Expand All @@ -28,6 +29,7 @@ import { DataSource } from 'typeorm';
return addTransactionalDataSource(new DataSource(options));
},
}),
VerificationModule
],
controllers: [],
providers: [],
Expand Down
10 changes: 9 additions & 1 deletion template/source/typeorm/main.ts → src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { initializeTransactionalContext } from 'typeorm-transactional';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
initializeTransactionalContext();

const app = await NestFactory.create(AppModule);
// TODO: 프로그램 구현
const config = new DocumentBuilder()
.setTitle('Phone Verification API')
.setDescription('API for phone number verification')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api-docs', app, document);

await app.listen(process.env.PORT || 8000);

console.log(`Application is running on: ${await app.getUrl()}`);
Expand Down
8 changes: 8 additions & 0 deletions src/verification/dto/send-verification.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IsNotEmpty, IsString, Matches } from 'class-validator';

export class SendVerificationDTO {
@IsNotEmpty()
@IsString()
@Matches(/^\d{3}-\d{4}-\d{4}$/)
phoneNumber: string;
}
13 changes: 13 additions & 0 deletions src/verification/dto/verify-code.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IsNotEmpty, IsString, Matches } from 'class-validator';

export class VerifyCodeDTO {
@IsNotEmpty()
@IsString()
@Matches(/^\d{3}-\d{4}-\d{4}$/)
phoneNumber: string;

@IsNotEmpty()
@IsString()
@Matches(/^\d{6}$/)
code: string;
}
9 changes: 9 additions & 0 deletions src/verification/veficitaion.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { VerificationController } from './verification.controller';
import { VerificationService } from './verification.service';

@Module({
controllers: [VerificationController],
providers: [VerificationService],
})
export class VerificationModule {}
34 changes: 34 additions & 0 deletions src/verification/verification.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Body, Controller, HttpException, HttpStatus, Post } from '@nestjs/common';
import { ApiBody, ApiTags } from '@nestjs/swagger';
import { validateSync } from 'class-validator';
import { VerificationService } from './verification.service';
import { SendVerificationDTO } from './dto/send-verification.dto';
import { VerifyCodeDTO } from './dto/verify-code.dto';

@ApiTags('Phone Verification')
@Controller('phone-verification')
export class VerificationController {
constructor(private readonly verificationService: VerificationService) {}

@Post('send-verification')
@ApiBody({ type: SendVerificationDTO })
sendVerification(@Body() body: SendVerificationDTO): { code: string } {
const errors = validateSync(body);
if (errors.length > 0) {
throw new HttpException('요청 바디의 값이 유효하지 않습니다.', HttpStatus.BAD_REQUEST);
}
const code = this.verificationService.sendVerificationCode(body.phoneNumber);
return { code };
}

@Post('verify-code')
@ApiBody({ type: VerifyCodeDTO })
verifyCode(@Body() body: VerifyCodeDTO): { result: boolean } {
const errors = validateSync(body);
if (errors.length > 0) {
throw new HttpException('요청 바디의 값이 유효하지 않습니다.', HttpStatus.BAD_REQUEST);
}
const result = this.verificationService.verifyCode(body.phoneNumber, body.code);
return { result };
}
}
29 changes: 29 additions & 0 deletions src/verification/verification.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
import { randomInt } from 'crypto';

interface VerificationStore {
[phoneNumber: string]: { code: string; expiresAt: number };
}

@Injectable()
export class VerificationService {
private verificationStore: VerificationStore = {};

sendVerificationCode(phoneNumber: string): string {
const code = randomInt(100000, 999999).toString();
const expiresAt = Date.now() + 5 * 60 * 1000; // 5분 후 만료
this.verificationStore[phoneNumber] = { code, expiresAt };
return code;
}

verifyCode(phoneNumber: string, code: string): boolean {
const verification = this.verificationStore[phoneNumber];
if (!verification || verification.expiresAt < Date.now()) {
throw new HttpException('인증번호가 만료되었거나 존재하지 않습니다.', HttpStatus.BAD_REQUEST);
}
if (verification.code !== code) {
throw new HttpException('인증번호가 일치하지 않습니다.', HttpStatus.BAD_REQUEST);
}
return true;
}
}
6 changes: 0 additions & 6 deletions template/environment/prisma/.env.example

This file was deleted.

39 changes: 0 additions & 39 deletions template/environment/prisma/docker-compose.yml

This file was deleted.

11 changes: 0 additions & 11 deletions template/environment/typeorm/.env.example

This file was deleted.

87 changes: 0 additions & 87 deletions template/package/package.prisma.json

This file was deleted.

12 changes: 0 additions & 12 deletions template/source/prisma-src/app.module.ts

This file was deleted.

12 changes: 0 additions & 12 deletions template/source/prisma-src/main.ts

This file was deleted.

15 changes: 0 additions & 15 deletions template/source/prisma-src/prisma/prisma.service.ts

This file was deleted.

23 changes: 0 additions & 23 deletions template/source/prisma-src/prisma/schema.prisma

This file was deleted.