From 5ebd907e1ca64dbec3671d4c8f381d48948ee368 Mon Sep 17 00:00:00 2001 From: ChangHoonOh Date: Thu, 18 Jan 2024 23:34:32 +0900 Subject: [PATCH] refactor/#108: using enum, error handling improved --- src/apis/auth/util/getSnsProfile.ts | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/apis/auth/util/getSnsProfile.ts b/src/apis/auth/util/getSnsProfile.ts index a5c613d8..31dd4118 100644 --- a/src/apis/auth/util/getSnsProfile.ts +++ b/src/apis/auth/util/getSnsProfile.ts @@ -1,9 +1,8 @@ import fetch from 'node-fetch'; import { GoogleUserResponse, KakaoUserResponse, NaverUserResponse, SnsProfileBase } from '../social/types/auth-social.type'; -import { HttpBadRequestException } from '@src/http-exceptions/exceptions/http-bad-request.exception'; -import { ERROR_CODE } from '@src/constants/error/error-code.constant'; import { HttpInternalServerErrorException } from '@src/http-exceptions/exceptions/http-internal-server-error.exception'; import { COMMON_ERROR_CODE } from '@src/constants/error/common/common-error-code.constant'; +import { UserLoginType } from '@src/apis/users/constants/user.enum'; /** * SNS에서 사용자 프로필 정보를 가져온다. @@ -11,12 +10,12 @@ import { COMMON_ERROR_CODE } from '@src/constants/error/common/common-error-code * @param {string} snsToken SNS에서 발급한 token * @returns {Promise} SNS profile 데이터 */ -export async function getSnsProfile(loginType: string, snsToken: string): Promise { +export async function getSnsProfile(loginType: UserLoginType, snsToken: string): Promise { try { let result: SnsProfileBase; switch (loginType) { - case 'KAKAO': { + case UserLoginType.Kakao: { const response = await fetch('https://kapi.kakao.com/v2/user/me', { method: 'GET', headers: { Authorization: `Bearer ${snsToken}` }, @@ -24,11 +23,11 @@ export async function getSnsProfile(loginType: string, snsToken: string): Promis const { id: kakaoId } = (await response.json()) as KakaoUserResponse; - result = { sns_id: kakaoId }; + result = { snsId: kakaoId }; break; } - case 'GOOGLE': { + case UserLoginType.Google: { const response = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', { method: 'GET', headers: { Authorization: `Bearer ${snsToken}` }, @@ -36,11 +35,11 @@ export async function getSnsProfile(loginType: string, snsToken: string): Promis const { sub: googleSub } = (await response.json()) as GoogleUserResponse; - result = { sns_id: googleSub }; + result = { snsId: googleSub }; break; } - case 'NAVER': { + case UserLoginType.Naver: { const response = await fetch('https://openapi.naver.com/v1/nid/me', { method: 'GET', headers: { Authorization: `Bearer ${snsToken}` }, @@ -51,23 +50,25 @@ export async function getSnsProfile(loginType: string, snsToken: string): Promis if (resultcode !== '00') { throw new HttpInternalServerErrorException({ code: COMMON_ERROR_CODE.SERVER_ERROR, - ctx: `네이버 서버 에러 ${message}` + ctx: '네이버 서버 에러', + stack: message }) } - result = { sns_id: naverResponse?.id || '' }; + result = { snsId: naverResponse?.id || '' }; break; } default: - throw new HttpBadRequestException({ - code: ERROR_CODE.INVALID_REQUEST_PARAMETER + throw new HttpInternalServerErrorException({ + code: COMMON_ERROR_CODE.SERVER_ERROR, + ctx: '소셜 프로필 조회 중 알 수 없는 에러', }); } return result; } catch (error) { - console.error('Error fetching user information:', error.message); + console.error('Error fetching getSnsProfile : ', error.message); return null; } }