Skip to content

Commit

Permalink
[NDD-254]: 개발용 토큰 반환 API 구현 (0.5h / 0.5h) (#106)
Browse files Browse the repository at this point in the history
* feat: 개발자용 토큰 발급 API controller, service 계층 메서드 구조 구현

* feat: 개발자용 토큰 반환 API 서비스 로직 구현

* feat: 개발자용 토큰 발급 API Authorization 해더로 반환되도록 구현

* docs: default API(/, 더미데이터 만드는 API)와 개발자용 토큰 반환하는 API Swagger 문서에서 제외

* docs: CD 스크립트 수정

* docs: docker run 옵션 추가
  • Loading branch information
quiet-honey authored Nov 25, 2023
1 parent a11e1a9 commit 97fb03e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/be-cd-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ on:
branches:
- dev

defaults:
run:
working-directory: ./BE

jobs:
build:
runs-on: self-hosted
Expand All @@ -20,4 +24,4 @@ jobs:
sudo docker build -t ${{secrets.BE_DOCKER_USERNAME}}/${{secrets.BE_DOCKER_REPO}}:${{secrets.DOCKER_DEV_TAG}} --platform linux/amd64 .
sudo docker push ${{secrets.BE_DOCKER_USERNAME}}/${{secrets.BE_DOCKER_REPO}}:${{secrets.DOCKER_DEV_TAG}}
- name: Run Docker Container
run: sudo docker run -d -p 8080:8080 -e TZ=Asia/Seoul --name ${{secrets.BE_DOCKER_CONTAINER}} ${{secrets.BE_DOCKER_USERNAME}}/${{secrets.BE_DOCKER_REPO}}:${{secrets.DOCKER_DEV_TAG}}
run: docker run --oom-kill-disable --restart on-failure -d -p 80:8080 -e TZ=Asia/Seoul --name ${{secrets.BE_DOCKER_CONTAINER}} ${{secrets.BE_DOCKER_USERNAME}}/${{secrets.BE_DOCKER_REPO}}:${{secrets.DOCKER_DEV_TAG}}
3 changes: 3 additions & 0 deletions BE/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { ApiExcludeEndpoint } from '@nestjs/swagger';

@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
@ApiExcludeEndpoint()
async getHello() {
return this.appService.getHello();
}

@Get('/initializeDummy')
@ApiExcludeEndpoint()
async saveDummy() {
await this.appService.saveDummy();
}
Expand Down
3 changes: 1 addition & 2 deletions BE/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { CategoryRepository } from '../../category/repository/category.repositor
import { QuestionRepository } from '../../question/repository/question.repository';
import { Category } from '../../category/entity/category';
import { Question } from '../../question/entity/question';

const BEARER_PREFIX: string = 'Bearer ';
import { BEARER_PREFIX } from 'src/constant/constant';

@Injectable()
export class AuthService {
Expand Down
2 changes: 2 additions & 0 deletions BE/src/constant/constant.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dotenv/config';

export const BEARER_PREFIX: string = 'Bearer ';

export const companies = [
'네이버',
'카카오',
Expand Down
4 changes: 4 additions & 0 deletions BE/src/token/service/token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class TokenService {
}
}

async getDevToken() {
return this.createToken(1); // 1번은 developndd 이메일로 가입한 개발자용 회원임
}

private async findByAccessToken(accessToken: string) {
return await this.tokenRepository.findByAccessToken(accessToken);
}
Expand Down
17 changes: 17 additions & 0 deletions BE/src/token/token.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Get, Res } from '@nestjs/common';
import { TokenService } from './service/token.service';
import { Response } from 'express';
import { BEARER_PREFIX } from 'src/constant/constant';
import { ApiExcludeEndpoint } from '@nestjs/swagger';

@Controller('/api/token')
export class TokenController {
constructor(private tokenService: TokenService) {}

@Get()
@ApiExcludeEndpoint()
async getDevToken(@Res() res: Response) {
const devToken = await this.tokenService.getDevToken();
res.setHeader('Authorization', `${BEARER_PREFIX} ${devToken}`).send();
}
}
2 changes: 2 additions & 0 deletions BE/src/token/token.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ConfigModule, ConfigService } from '@nestjs/config';
import { JwtModule } from '@nestjs/jwt';
import 'dotenv/config';
import { AccessTokenStrategy } from './strategy/access.token.strategy';
import { TokenController } from './token.controller';

@Module({
imports: [
Expand All @@ -30,5 +31,6 @@ import { AccessTokenStrategy } from './strategy/access.token.strategy';
AccessTokenStrategy,
],
exports: [TokenService],
controllers: [TokenController],
})
export class TokenModule {}

0 comments on commit 97fb03e

Please sign in to comment.