diff --git a/package.json b/package.json index cc52d5d..59bee21 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "dependencies": { "@langchain/core": "^0.3.51", "@langchain/openai": "^0.5.10", + "@nestjs/axios": "^4.0.0", "@nestjs/common": "^11.0.1", "@nestjs/config": "^4.0.2", "@nestjs/core": "^11.0.1", diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index e4fe0c1..3fc98d4 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -1,3 +1,4 @@ +import { HttpModule } from '@nestjs/axios'; import { Module } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; @@ -11,6 +12,7 @@ import { JwtStrategy } from './strategy/jwt.strategy'; @Module({ imports: [ + HttpModule.register({}), PassportModule, UserModule, JwtModule.register({ diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 7a03e37..b6de08f 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,7 +1,8 @@ +import { HttpService } from '@nestjs/axios'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { JwtService } from '@nestjs/jwt'; -import axios, { AxiosError } from 'axios'; +import { catchError, firstValueFrom } from 'rxjs'; import { KakaoUserResponse, UserPayload } from '@gglk/auth/auth.interface'; import { UserService } from '@gglk/user/user.service'; import { TOKEN_TYPE } from './auth.constant'; @@ -13,69 +14,65 @@ export class AuthService { private readonly jwtService: JwtService, private readonly userService: UserService, private readonly configService: ConfigService, + private readonly httpService: HttpService, ) {} - async getKakaoUserAccessToken(code: string, redirectUri: string) { - try { - const data = new URLSearchParams(); - data.append('grant_type', 'authorization_code'); - data.append( - 'client_id', - this.configService.get('KAKAO_CLIENT_ID')!, - ); - data.append('code', code); - data.append('redirect_uri', redirectUri); + private get KAKAO_GRANT_TYPE() { + return 'authorization_code'; + } - const response = await axios.post( - 'https://kauth.kakao.com/oauth/token', - data, - { - headers: { - 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', + async getKakaoUserAccessToken(code: string, redirect_uri: string) { + const data = new URLSearchParams({ + grant_type: this.KAKAO_GRANT_TYPE, + client_id: this.configService.get('KAKAO_CLIENT_ID')!, + code, + redirect_uri, + }); + + const response = await firstValueFrom( + this.httpService + .post<{ access_token: string; [k: string]: unknown }>( + 'https://kauth.kakao.com/oauth/token', + data, + { + headers: { + 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', + }, }, - }, - ); - const { access_token }: { access_token: string; [k: string]: unknown } = - response.data; - return access_token; - } catch (e) { - if (e instanceof AxiosError) { - console.error( - 'Error while making authentication to Kakao - Get bearer token', - ); - throw new KakaoOauthException(); - } else { - throw e; - } - } + ) + .pipe( + catchError((error) => { + console.error(error); + throw new KakaoOauthException(); + }), + ), + ); + + return response.data.access_token; } async getKakaoUserByAccessToken(accessToken: string) { - try { - const userRes = await axios.get( - 'https://kapi.kakao.com/v2/user/me', - { + const userRes = await firstValueFrom( + this.httpService + .get('https://kapi.kakao.com/v2/user/me', { headers: { Authorization: `Bearer ${accessToken}`, }, - }, - ); + }) + .pipe( + catchError((error) => { + console.error(error); + throw new KakaoOauthException(); + }), + ), + ); - const kakaoUser = userRes.data; - return { - id: kakaoUser.id.toString(), - name: kakaoUser.properties?.nickname ?? '', - }; - } catch (e) { - if (e instanceof AxiosError) { - console.error( - 'Error while making authentication to Kakao - Get user information', - ); - throw new KakaoOauthException(); - } else { - throw e; - } - } + const { data: kakaoUser } = userRes; + + return { + id: kakaoUser.id.toString(), + name: kakaoUser.properties?.nickname ?? '', + }; } async generateGuestToken() { diff --git a/yarn.lock b/yarn.lock index 1f44af5..73e7ad5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1028,6 +1028,11 @@ "@napi-rs/nice-win32-ia32-msvc" "1.0.1" "@napi-rs/nice-win32-x64-msvc" "1.0.1" +"@nestjs/axios@^4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@nestjs/axios/-/axios-4.0.0.tgz#520ff7c6238e635658fe334ee62db9d1b9c5546f" + integrity sha512-1cB+Jyltu/uUPNQrpUimRHEQHrnQrpLzVj6dU3dgn6iDDDdahr10TgHFGTmw5VuJ9GzKZsCLDL78VSwJAs/9JQ== + "@nestjs/cli@^11.0.0": version "11.0.7" resolved "https://registry.yarnpkg.com/@nestjs/cli/-/cli-11.0.7.tgz#204a1969c2609d7cf5e98a4d65819bd8585bead7"