Skip to content

Commit

Permalink
Merge pull request #523 from boostcampwm2023/BE/feature/#522-데이터베이스-최적화
Browse files Browse the repository at this point in the history
Be/feature/#522 데이터베이스 최적화
  • Loading branch information
kimyu0218 authored Feb 14, 2024
2 parents 0db74d8 + d42e1be commit 85e56b3
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 411 deletions.
10 changes: 5 additions & 5 deletions backend/was/src/auth/__mocks__/kakao.auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Cache } from 'cache-manager';
import { ERR_MSG } from 'src/common/constants/errors';
import { PROVIDER_ID, PROVIDER_NAME } from 'src/common/constants/etc';
import { ProviderIdEnum, ProviderName } from 'src/common/constants/etc';
import { Member } from 'src/members/entities';
import { Repository } from 'typeorm';
import { JwtPayloadDto, OAuthTokenDto, ProfileDto } from '../dto';
Expand Down Expand Up @@ -59,26 +59,26 @@ export class MockedKakaoAuthService extends AuthService {
readonly cacheManager: Cache,
) {
super(jwtService, membersRepository, cacheManager);
this.init(PROVIDER_NAME.KAKAO);
this.init(ProviderName.KAKAO);
}

async loginOAuth(code: string): Promise<string> {
const token: KakaoTokenDto = this.requestToken(code);
const profile: ProfileDto = this.getUser(token.access_token ?? '');
const member: Member | null = await this.findMemberByEmail(
profile.email,
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
);
if (member) {
return this.login(
member.id,
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
profile,
OAuthTokenDto.fromKakao(token),
);
}
return this.signup(
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
profile,
OAuthTokenDto.fromKakao(token),
);
Expand Down
8 changes: 4 additions & 4 deletions backend/was/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { ApiTags } from '@nestjs/swagger';
import * as dotenv from 'dotenv';
import { Request, Response } from 'express';
import { PROVIDER_ID } from 'src/common/constants/etc';
import { ProviderIdEnum } from 'src/common/constants/etc';
import {
AuthenticateDecorator,
KakaoLoginDecorator,
Expand Down Expand Up @@ -60,12 +60,12 @@ export class AuthController {
async kakaoLogout(@Req() req: any, @Res() res: Response): Promise<void> {
const user: JwtPayloadDto = req.user;
switch (user.providerId) {
case PROVIDER_ID.KAKAO:
case ProviderIdEnum.KAKAO:
await this.kakaoAuthService.logoutOAuth(user);
break;
case PROVIDER_ID.NAVER:
case ProviderIdEnum.NAVER:
break;
case PROVIDER_ID.GOOGLE:
case ProviderIdEnum.GOOGLE:
break;
}
res.clearCookie('magicconch');
Expand Down
14 changes: 7 additions & 7 deletions backend/was/src/auth/service/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import * as dotenv from 'dotenv';
import { RedisCacheModule } from 'src/common/config/cache/redis-cache.module';
import { PROVIDER_ID } from 'src/common/constants/etc';
import { ProviderIdEnum } from 'src/common/constants/etc';
import { CreateMemberDto, UpdateMemberDto } from 'src/members/dto';
import { Member } from 'src/members/entities';
import { Repository } from 'typeorm';
Expand Down Expand Up @@ -67,17 +67,17 @@ describe('AuthService', () => {
it('신규회원은 회원가입 할 수 있다.', async () => {
[
{
providerId: PROVIDER_ID.KAKAO,
providerId: ProviderIdEnum.KAKAO,
profile: profileDto,
token: oauthTokenDto,
},
{
providerId: PROVIDER_ID.NAVER,
providerId: ProviderIdEnum.NAVER,
profile: profileDto,
token: oauthTokenDto,
},
{
providerId: PROVIDER_ID.GOOGLE,
providerId: ProviderIdEnum.GOOGLE,
profile: profileDto,
token: oauthTokenDto,
},
Expand Down Expand Up @@ -105,19 +105,19 @@ describe('AuthService', () => {
[
{
id: 'id1',
providerId: PROVIDER_ID.KAKAO,
providerId: ProviderIdEnum.KAKAO,
profile: profileDto,
token: oauthTokenDto,
},
{
id: 'id2',
providerId: PROVIDER_ID.NAVER,
providerId: ProviderIdEnum.NAVER,
profile: profileDto,
token: oauthTokenDto,
},
{
id: 'id3',
providerId: PROVIDER_ID.GOOGLE,
providerId: ProviderIdEnum.GOOGLE,
profile: profileDto,
token: oauthTokenDto,
},
Expand Down
9 changes: 6 additions & 3 deletions backend/was/src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ export class AuthService {
providerId: number,
): Promise<Member | null> {
try {
return await this.membersRepository.findOneBy({
email: email,
providerId: providerId,
return await this.membersRepository.findOne({
where: {
email: email,
providerId: providerId,
},
select: ['id'],
});
} catch (err: unknown) {
throw err;
Expand Down
10 changes: 5 additions & 5 deletions backend/was/src/auth/service/kakao.auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Cache } from 'cache-manager';
import { CONTENT_TYPE, METHODS, OAUTH_URL } from 'src/common/constants/apis';
import { ERR_MSG } from 'src/common/constants/errors';
import { PROVIDER_ID, PROVIDER_NAME } from 'src/common/constants/etc';
import { ProviderIdEnum } from 'src/common/constants/etc';
import { Member } from 'src/members/entities';
import { Repository } from 'typeorm';
import { JwtPayloadDto, OAuthTokenDto, ProfileDto } from '../dto';
Expand All @@ -30,26 +30,26 @@ export class KakaoAuthService extends AuthService {
readonly cacheManager: Cache,
) {
super(jwtService, membersRepository, cacheManager);
this.init(PROVIDER_NAME.KAKAO);
this.init('KAKAO');
}

async loginOAuth(code: string): Promise<string> {
const token: KakaoTokenDto = await this.requestToken(code);
const profile: ProfileDto = await this.getUser(token.access_token ?? '');
const member: Member | null = await this.findMemberByEmail(
profile.email,
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
);
if (member) {
return this.login(
member.id,
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
profile,
OAuthTokenDto.fromKakao(token),
);
}
return this.signup(
PROVIDER_ID.KAKAO,
ProviderIdEnum.KAKAO,
profile,
OAuthTokenDto.fromKakao(token),
);
Expand Down
Loading

0 comments on commit 85e56b3

Please sign in to comment.