Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Fix: REST API 아키텍처 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed Mar 20, 2022
1 parent cd7375b commit 678910d
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 257 deletions.
15 changes: 0 additions & 15 deletions src/api/account/account.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,6 @@ const updatePWByCode = async (memberCode, memberPw) => {
return salt;
}

const getToken = async (token) => {
const getTokenQuery="SELECT * FROM `tokens` WHERE `token`=? AND `valid`=1";
try{
const [rows] = await pool.query(getTokenQuery, [token]);
if(rows.length)
return rows[0];
else
return null;
}catch(err){
console.error(err);
throw new InternalServerException();
}
}

module.exports = {
getMemberById,
getMemberByCode,
Expand All @@ -174,5 +160,4 @@ module.exports = {
getStudentInfoByCode,
updateCodeAvailable,
updatePWByCode,
getToken
}
3 changes: 2 additions & 1 deletion src/api/account/account.service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { NotFoundException, BadRequestException, UnAuthorizedException, ConflictException, InternalServerException } = require('../../util/exceptions');
const repository = require('./account.repository');
const tokenRepository = require('./token.repository');
const jwt = require('../../util/jwt');
const crypto = require('crypto');
const sharp = require('sharp');
Expand Down Expand Up @@ -258,7 +259,7 @@ const token = async (refreshToken) => {
throw new UnAuthorizedException();
}
// db에서 리프레시 토큰 사용이 가능한지 확인
const tokenInfo = await repository.getToken(result.token);
const tokenInfo = await tokenRepository.getToken(result.token);
// 리프레시 토큰이 db에서 사용불가 되었으면
if(tokenInfo === null){
throw new UnAuthorizedException();
Expand Down
36 changes: 36 additions & 0 deletions src/api/account/token.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { InternalServerException } from '../../util/exceptions';
const pool = require('../../util/db');

const getToken = async (
token: string,
) => {
const getTokenQuery="SELECT * FROM `tokens` WHERE `token`=? AND `valid`=1";
try{
const [rows] = await pool.query(getTokenQuery, [token]);
if(rows.length)
return rows[0];
else
return null;
}catch(err){
console.error(err);
throw new InternalServerException();
}
}

const insertToken = async (
token: string,
memberCode: number
) => {
const insertTokenQuery="INSERT INTO `tokens` VALUES(?, 1, ?, now())";
try{
await pool.query(insertTokenQuery, [token, memberCode]);
}catch(err){
console.error(err);
throw new InternalServerException();
}
}

export {
getToken,
insertToken
}
23 changes: 10 additions & 13 deletions src/api/api.controller.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
const express = require('express')
const router = express.Router()
const jwt = require('../util/jwt')
const multer = require('multer')
const express = require('express');
const router = express.Router();
const jwt = require('../util/jwt');
const multer = require('multer');

const loginCheck = (req, res, next) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin){
return res.send(JSON.stringify(jwtValue.msg));
next(new UnAuthorizedException());
}
next();
}
const imageUpload = multer({
storage:multer.diskStorage({
destination:(req, file, cb) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin) return;
cb(null, 'public/resource/board/upload_images/')
cb(null, 'public/resource/board/upload_images/');
},
filename:(req, file, cb) => {
cb(null, Date.now()+'.'+file.originalname.split('.')[file.originalname.split('.').length-1])
cb(null, Date.now()+'.'+file.originalname.split('.')[file.originalname.split('.').length-1]);
}
})
})
const profileUpload = multer({
storage:multer.diskStorage({
destination:(req, file, cb) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin) return;
cb(null, 'public/resource/member/profile_images/')
cb(null, 'public/resource/member/profile_images/');
},
filename:(req, file, cb) => {
const jwtValue = jwt.check(req.cookies.token);
cb(null, 'temp-profile_'+jwtValue.memberCode+'.'+file.originalname.split('.')[file.originalname.split('.').length-1])
cb(null, 'temp-profile_'+jwtValue.memberCode+'.'+file.originalname.split('.')[file.originalname.split('.').length-1]);
}
})
})
Expand All @@ -52,6 +48,7 @@ const commentController = require('./board/comment.controller')
const likeController = require('./board/like.controller')
const imageUploadController = require('./board/imageUpload.controller')
const emoticonController = require('./board/emoticon.controller')
const { UnAuthorizedException } = require('../util/exceptions')

router.post('/account/login', accountController.login)
router.delete('/account/logout', accountController.logout)
Expand Down
20 changes: 3 additions & 17 deletions src/controller.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
const express = require('express');
const router = express.Router();
const jwt = require('./util/jwt');

const apiRouter = require('./api/api.controller');
const viewRouter = require('./view/view.controller');

// 업데이트 확인 url 하위호환
const versionController = require('./api/version/version.controller');
router.post('/database', versionController.getVersionLegacy);// 업데이트 확인 url 하위호환

router.post('/database', versionController.getVersionLegacy);

router.use('/api', apiRouter);
router.use('/', viewRouter);


router.use((req, res) => {
const jwtValue = jwt.check(req.cookies.token);
res.status(404).render('404', {
member:{
isLogin:jwtValue.isLogin,
code:jwtValue.memberCode,
id:jwtValue.memberId,
nickname:jwtValue.memberNickname,
level:jwtValue.memberLevel,
grade:jwtValue.grade,
classNo:jwtValue.classNo,
studentNo:jwtValue.studentNo,
}
})
res.status(404).render('404');
})

module.exports = router;
10 changes: 8 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ app.use('/', controller);

app.use(function (err, req, res, next) {
if(err.httpCode){
res.status(err.httpCode).send(JSON.stringify({'statusCode':err.httpCode,'message':err.message}))
res.status(err.httpCode).send(JSON.stringify({
statusCode:err.httpCode,
message:err.message
}));
}else{
console.error(err);
res.status(500).send('Internal Server Error');
res.status(500).send(JSON.stringify({
statusCode:500,
message:'Internal Server Error'
}));
}
});

Expand Down
Loading

0 comments on commit 678910d

Please sign in to comment.