Skip to content

Commit

Permalink
Merge pull request #7 from netCard-TUK/hm
Browse files Browse the repository at this point in the history
유저 정보 예외 처리
  • Loading branch information
ohamin26 authored Jan 19, 2024
2 parents 6d9a455 + 1595c01 commit b0599b9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dist-ssr
*.sln
*.sw?

/public
/storage
.env
/package-lock.json
Expand Down
79 changes: 63 additions & 16 deletions src/api/cards/controller.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
const repository = require("./repogitory");
const repogitory_user = require("../user/repogitory");
const userRepogitory = require("../user/repogitory");
const jwt = require("jsonwebtoken");

//내 명함 정보 등록
exports.register = async (req, res) => {
const { position, organization, address, tell, email } = req.body;
const { user_id, position, organization, address, tell, email } = req.body;
const file = req.files;

// 확장자까지 넣기
// 사진 경로 받기
const photo = "http://" + req.get("host") + "/" + file["file"][0].filename;
let time = new Date();
const photo =
"http://" +
req.get("host") +
"/" +
file["file"][0].filename +
time.getTime();

// jwt 토큰 값 받고 id 값만 분리하기 - db에 user_id에 저장하기 위함
const { access_token } = req.headers;
//const [tokenType, tokenValue] = authorization.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);

checkUserInfo(res, user_id, id);

//db에 저장 정상적으로 저장 시 ok / 실패 시 fail
const { affectedRows, insertId } = await repository.create(
(user_id = id),
Expand All @@ -41,24 +49,18 @@ exports.inquiry = async (req, res) => {
const { access_token } = req.headers;
//const [tokenType, tokenValue] = authorization.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
const { user_id } = req.body;

checkUserInfo(res, user_id, id);

// 명함 정보 가져오기
// 명함 정보 가져오기
const item = await repository.show({ cardId, userId: id });
if (item == null) {
if (item === null) {
return res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(cardId나 userId를 확인해주세요)",
});
}
//유저 정보 가져오기
const user_info = await repogitory_user.show_user(item.user_id);
if (user_info == null) {
return res.send({
isSuccess: "false",
message: "조회된 값이 없습니다(user_Id를 확인해주세요)",
});
}

const response = {
isSuccess: "true",
position: item.position,
Expand All @@ -74,12 +76,16 @@ exports.inquiry = async (req, res) => {
res.send(response);
};

//내 명함 정보 전체 조회
exports.inquiry_all = async (req, res) => {
//user_id 값 가져오기
const { access_token } = req.headers;
const { user_id } = req.body;
//const [tokenType, tokenValue] = access_token.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);

checkUserInfo(res, user_id, id);

const item_all = await repository.show_all(id);

res.send({
Expand All @@ -101,7 +107,7 @@ exports.inquiry_other = async (req, res) => {
});
}
//유저 정보 가져오기
const user_info = await repogitory_user.show_user(item.user_id);
const user_info = await userRepogitory.show_user(item.user_id);
if (user_info == null) {
return res.send({
isSuccess: "false",
Expand All @@ -128,8 +134,13 @@ exports.inquiry_other = async (req, res) => {
exports.update = async (req, res) => {
const cardId = req.params.cardId;

let { position, organization, address, photo, tell, email } = req.body;
const { access_token } = req.headers;
//const [tokenType, tokenValue] = access_token.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
let { user_id, position, organization, address, photo, tell, email } =
req.body;

checkUserInfo(res, user_id, id);
//명함 정보 가져오기
const item = await repository.show(cardId);
if (item == null) {
Expand Down Expand Up @@ -166,6 +177,12 @@ exports.update = async (req, res) => {
//내 명함 목록 삭제
exports.delete = async (req, res) => {
const cardId = req.params.cardId;
const { access_token } = req.headers;
//const [tokenType, tokenValue] = access_token.split(" ");
const { id } = jwt.verify(access_token, process.env.JWT_KEY);
let { user_id } = req.body;

checkUserInfo(res, user_id, id);

const { affectedRows, insertId } = await repository.delete(cardId);

Expand All @@ -175,3 +192,33 @@ exports.delete = async (req, res) => {
res.send({ isSuccess: "false", message: "삭제 실패" });
}
};

const checkUserInfo = async (res, userId, id) => {
// 유저 정보 확인하기
if (userId != id) {
return res.send({
isSuccess: "false",
message: "올바른 토큰 값이 아닙니다.",
});
}

// user_id, id 타입 일치 확인
if (typeof userId !== typeof id) {
return res.send({
isSuccess: "false",
message: "타입이 일치하지 않습니다.(user_id 타입은 int형 입니다.)",
});
}

// 유저 정보 가져오기
const user_info = await userRepogitory.show_user(item.user_id);
if (user_info === null) {
return res.send({
isSuccess: "false",
message: `조회된 값이 없습니다(${errorMessage})`,
});
}

// 함수를 통과했다면, 유효한 정보 반환
return { item, user_info };
};

0 comments on commit b0599b9

Please sign in to comment.