Skip to content

Commit

Permalink
회원가입 및 로그인 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
wook-hyung committed Dec 14, 2023
1 parent 0858401 commit 5cc4feb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
25 changes: 14 additions & 11 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@ const dotenv = require('dotenv');
dotenv.config();

const { userService } = require('../services/userService');
const { getUserInfoFromKakao } = require('../lib/kakao');

const userController = {
signIn: async (req, res) => {
const accessToken = req.body.accessToken;
// 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)
Expand Down Expand Up @@ -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,
});
}
},

Expand Down
18 changes: 18 additions & 0 deletions lib/kakao.js
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 1 addition & 1 deletion models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions routes/userRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "회원가입 요청에 성공했습니다."
Expand Down
17 changes: 12 additions & 5 deletions services/userService.js
Original file line number Diff line number Diff line change
@@ -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;
},
Expand All @@ -18,7 +19,7 @@ const userService = {
console.log('토큰생성시작', key);
const expiresIn = '10d'; //토큰 유효 기간

let token = jwt.sign(
const token = jwt.sign(
{
id,
nickname,
Expand All @@ -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;
},
};

Expand Down

0 comments on commit 5cc4feb

Please sign in to comment.