Skip to content

Commit

Permalink
Feat : accessToken 관련 에러 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
namewhat99 committed Apr 17, 2024
1 parent 52d4625 commit 2009e55
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/stat/service/github.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BadRequestException,
Injectable,
InternalServerErrorException,
Logger,
NotFoundException,
} from '@nestjs/common';
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/stat/stat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Param,
Patch,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { AlgorithmService } from './service/algorithm.service';
Expand Down Expand Up @@ -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);
// }
}

0 comments on commit 2009e55

Please sign in to comment.