From 5cc4febbcc5964a301664e6cff09b252a1e97f47 Mon Sep 17 00:00:00 2001 From: HyungWookChoi Date: Thu, 14 Dec 2023 11:04:07 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=9B=90=EA=B0=80=EC=9E=85=20?= =?UTF-8?q?=EB=B0=8F=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/userController.js | 25 ++++++++++++++----------- lib/kakao.js | 18 ++++++++++++++++++ models/userModel.js | 2 +- routes/userRouter.js | 10 +++++----- services/userService.js | 17 ++++++++++++----- 5 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 lib/kakao.js diff --git a/controllers/userController.js b/controllers/userController.js index a2683cd..93eecc7 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -3,6 +3,7 @@ const dotenv = require('dotenv'); dotenv.config(); const { userService } = require('../services/userService'); +const { getUserInfoFromKakao } = require('../lib/kakao'); const userController = { signIn: async (req, res) => { @@ -10,17 +11,12 @@ const userController = { // 1. 토큰 정보 받아오기(실패시 오류) -> 2. 토큰 정보의 provider_id DB조회 -> 3. provider_id가 없으면 회원가입 후 로그인(있으면 바로 로그인) try { // 1. 토큰 정보 받아오기 - const result = await axios.get('https://kapi.kakao.com/v2/user/me', { - headers: { - // Authorization: `Bearer ${process.env.KaKao_Tokken}`, - Authorization: `Bearer ${accessToken}`, - }, - }); - - const provider_id = result.data.id; + const { + data: { id: provider_id }, + } = getUserInfoFromKakao({ accessToken }); // 2. 토큰 정보의 provider_id DB조회 - const userData = await userService.getUserByproviderId(provider_id); + const userData = await userService.getUserByProviderId(provider_id); console.log(provider_id); // 3.provider_id가 있으면 토큰만듦(없으면 null) @@ -56,9 +52,16 @@ const userController = { signUp: async (req, res) => { const result = await userService.signUp(req.body); if (result.success) { - return res.status(201).json({ result: result.result }); + return res.status(201).json({ + success: true, + message: '회원가입 요청에 성공했습니다.', + }); } else { - return res.status(400).json({ result: result.result }); + return res.status(400).json({ + success: false, + message: '회원가입 요청에 실패했습니다.', + err: result.err.message, + }); } }, diff --git a/lib/kakao.js b/lib/kakao.js new file mode 100644 index 0000000..7c1ca03 --- /dev/null +++ b/lib/kakao.js @@ -0,0 +1,18 @@ +const axios = require('axios'); + +const getUserInfoFromKakao = async ({ accessToken }) => { + try { + const result = await axios.get('https://kapi.kakao.com/v2/user/me', { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }); + return result; + } catch (err) { + throw err; + } +}; + +module.exports = { + getUserInfoFromKakao, +}; diff --git a/models/userModel.js b/models/userModel.js index 445605f..24e79d6 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -2,7 +2,7 @@ const { pool } = require('./pool'); const userModel = { // 토큰 정보의 provider_id DB조회 - getUserByproviderId: async (provider_id) => { + getUserByProviderId: async (provider_id) => { const connection = await pool.getConnection(); try { diff --git a/routes/userRouter.js b/routes/userRouter.js index cc0f05a..343fd71 100644 --- a/routes/userRouter.js +++ b/routes/userRouter.js @@ -77,23 +77,23 @@ router.post('/signIn', userController.signIn); * post: * tags: [Users] * summary: "회원가입" - * description: "회원가입(nickname, provider_id, champion 값 필요), champion 값은 CHAMP1, CHAMP2, CHAMP3, CHAMP4, CHAMP5 중 하나여야 함" + * description: "회원가입(accessToken, nickname, champion 값 필요), champion 값은 RED, YELLOW, GREEN, BLUE, PINK, BEIGE 중 하나여야 함" * requestBody: * content: * application/json: * schema: * type: object - * example: {"nickname": "닉네임", "provider_id": "123456789", "champion": "CHAMP1"} + * example: {"nickname": "닉네임", "accessToken": "123456789", "champion": "CHAMP1"} * properties: * nickname: * type: string * description: "닉네임" - * provider_id: + * accessToken: * type: string - * description: "카카오 provider_id" + * description: "카카오 access 토큰" * champion: * type: string - * description: "챔피언 이름(CHAMP1, CHAMP2, CHAMP3, CHAMP4, CHAMP5)" + * description: "챔피언 이름(RED, YELLOW, GREEN, BLUE, PINK, BEIGE)" * responses: * "201": * description: "회원가입 요청에 성공했습니다." diff --git a/services/userService.js b/services/userService.js index e5b31c6..68c98bb 100644 --- a/services/userService.js +++ b/services/userService.js @@ -1,10 +1,11 @@ +const { getUserInfoFromKakao } = require('../lib/kakao'); const { userModel } = require('../models/userModel'); const jwt = require('jsonwebtoken'); const userService = { // 토큰 정보의 provider_id DB조회 - getUserByproviderId: async (provider_id) => { - const result = await userModel.getUserByproviderId(provider_id); + getUserByProviderId: async (provider_id) => { + const result = await userModel.getUserByProviderId(provider_id); return result; }, @@ -18,7 +19,7 @@ const userService = { console.log('토큰생성시작', key); const expiresIn = '10d'; //토큰 유효 기간 - let token = jwt.sign( + const token = jwt.sign( { id, nickname, @@ -40,9 +41,15 @@ const userService = { // 회원가입 signUp: async (userData) => { - const result = await userModel.signUp(userData); + const { + data: { id: provider_id }, + } = await getUserInfoFromKakao(userData); + const result = await userModel.signUp({ + ...userData, + provider_id, + }); - return { result }; + return result; }, };