From 2009e55a2adf499a5ba5013623b117e07059cfe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=80=E1=85=AA=E1=86=BC=E1=84=92?= =?UTF-8?q?=E1=85=AE=E1=86=AB?= Date: Wed, 17 Apr 2024 12:34:16 +0900 Subject: [PATCH] =?UTF-8?q?Feat=20:=20accessToken=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/stat/service/github.service.ts | 43 +++++++++++++++++------------- src/stat/stat.controller.ts | 6 +++++ 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/src/stat/service/github.service.ts b/src/stat/service/github.service.ts index bc13d31..607877f 100644 --- a/src/stat/service/github.service.ts +++ b/src/stat/service/github.service.ts @@ -1,6 +1,7 @@ import { BadRequestException, Injectable, + InternalServerErrorException, Logger, NotFoundException, } from '@nestjs/common'; @@ -112,35 +113,39 @@ export class GithubService { }); const userResourceJson = await userResource.json(); - if (!userResourceJson.message) return userResourceJson; else { if (userResourceJson.message === 'Bad credentials') { this.logger.error('AccessToken 에 문제가 있음'); + throw new BadRequestException('잘못된 accessToken 입니다'); } else if (userResourceJson.message === 'Requires authentication') { this.logger.error( 'Auth Header 에 AccessToken 이 포함되지 않음', ); + throw new InternalServerErrorException( + 'Auth Header 에 AccessToken 이 포함되지 않음', + ); } } } - // public async redirect(code: string) { - // const accessToken = await this.fetchAccessToken(code); - // const userResource = await this.getUserResource(accessToken); - // const isExist = await this.githubRepository.findOne(userResource.id); - // - // if (isExist) { - // throw new BadRequestException('이미 등록된 id 입니다'); - // } - // - // const githubPoint = this.calculateGithubPoint(userResource); - // - // const github = new Github(); - // github.userId = '123'; - // github.point = githubPoint; - // github.accessToken = accessToken; - // github.githubId = userResource.id; - // await this.githubRepository.save(github); - // } + public async redirect(code: string) { + const accessToken = await this.fetchAccessToken(code); + console.log(accessToken); + const userResource = await this.getUserResource(accessToken); + const isExist = await this.githubRepository.findOne(userResource.id); + + if (isExist) { + throw new BadRequestException('이미 등록된 id 입니다'); + } + + const githubPoint = this.calculateGithubPoint(userResource); + + const github = new Github(); + github.userId = '123'; + github.point = githubPoint; + github.accessToken = accessToken; + github.githubId = userResource.id; + await this.githubRepository.save(github); + } } diff --git a/src/stat/stat.controller.ts b/src/stat/stat.controller.ts index a52ed83..9f46d7e 100644 --- a/src/stat/stat.controller.ts +++ b/src/stat/stat.controller.ts @@ -6,6 +6,7 @@ import { Param, Patch, Post, + Query, UseGuards, } from '@nestjs/common'; import { AlgorithmService } from './service/algorithm.service'; @@ -105,4 +106,9 @@ export class StatController { await this.gradeService.gradeDelete(userId); await this.totalService.updateTotal(userId); } + + // @Get('github/redirect') + // public async redirect(@Query('code') code: string) { + // await this.githubService.redirect(code); + // } }