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

Commit

Permalink
Fix: 토큰 재발급 버그, 코드 리팩토링 및 최적화, Remove: 검색 api
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed Mar 21, 2022
1 parent 678910d commit b06b8e6
Show file tree
Hide file tree
Showing 50 changed files with 595 additions and 671 deletions.
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "BSM",
"version": "1.3.0",
"version": "1.4.0",
"dependencies": {
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.6",
"@types/multer": "^1.4.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
Expand Down
113 changes: 59 additions & 54 deletions src/api/account/account.controller.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const express = require('express');
const router = express.Router();
const { BadRequestException, UnAuthorizedException } = require('../../util/exceptions');
const service = require('./account.service')
const jwt = require('../../util/jwt')
const multer = require('multer');

const login = async (req, res, next) =>{
router.post('/account/login', async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.login(res, req.body.member_id, req.body.member_pw)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const logout = (req, res) =>{
router.delete('/account/logout', (req, res) => {
res.clearCookie('token', {
domain:'bssm.kro.kr',
path:'/',
Expand All @@ -30,112 +33,114 @@ const logout = (req, res) =>{
path:'/',
});
res.send();
}
})

const islogin = (req, res) =>{
if(jwt.check(req.cookies.token).isLogin){
router.get('/account/islogin', (req, res) => {
if (jwt.check(req.cookies.token).isLogin) {
res.send({islogin:true});
}else{
} else {
res.send({islogin:false});
}
}
})

const signUp = async (req, res, next) =>{
router.post('/account/signUp', async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.signUp(req.body.member_id, req.body.member_pw, req.body.member_pw_check, req.body.member_nickname, req.body.code)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const view = async (req, res, next) =>{
router.get('/account/:memberCode', async (req, res, next) => {
const jwtValue = jwt.check(req.cookies.token);
try {
res.send(JSON.stringify(
await service.viewUser(jwtValue.memberCode, req.params.memberCode)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const profileUpload = async (req, res, next) =>{
try {
res.send(JSON.stringify(
await service.profileUpload(req.file.filename)
));
}catch(err){
next(err);
}
}

const validCode = async (req, res, next) =>{
router.post('/account/validCode', async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.validCodeMail(req.body.student_enrolled, req.body.student_grade, req.body.student_class, req.body.student_no, req.body.student_name)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const pwResetMail = async (req, res, next) => {
router.post('/account/pwResetMail', async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.pwResetMail(req.body.member_id)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const pwEdit = async (req, res, next) =>{
router.post('/account/pwEdit', async (req, res, next) => {
const jwtValue = jwt.verify(req.cookies.token);
try {
if(jwtValue=='EXPIRED'){
if (jwtValue=='EXPIRED') {
throw new UnAuthorizedException('Token expired');
}
if(!(jwtValue.pwEdit||jwtValue.memberCode)){
if (!(jwtValue.pwEdit||jwtValue.memberCode)) {
BadRequestException();
}
}catch(err){
} catch(err) {
next(err);
}

let memberCode;
if(jwtValue.memberCode) memberCode = jwtValue.memberCode;
if(jwtValue.pwEdit) memberCode = jwtValue.pwEdit;
if (jwtValue.memberCode) memberCode = jwtValue.memberCode;
if (jwtValue.pwEdit) memberCode = jwtValue.pwEdit;

try {
res.send(JSON.stringify(
await service.pwEdit(res, memberCode, req.body.member_pw, req.body.member_pw_check)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

const token = async (req, res, next) => {
router.post('/account/token', async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.token(req.body.refreshToken)
));
}catch(err){
} catch(err) {
next(err);
}
}
})

router.post('/profileUpload',
multer({
storage:multer.diskStorage({
destination:(req, file, cb) => {
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]);
}
})
}).single('file'),
async (req, res, next) => {
try {
res.send(JSON.stringify(
await service.profileUpload(req.file.filename)
));
} catch(err) {
next(err);
}
}
)

module.exports = {
login,
logout,
islogin,
signUp,
view,
profileUpload,
validCode,
pwResetMail,
pwEdit,
token
}
module.exports = router;
48 changes: 24 additions & 24 deletions src/api/account/account.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,93 @@ const crypto = require('crypto');

const getMemberById = async (memberId) => {
const getMemberQuery="SELECT * FROM `members` WHERE `member_id`=?";
try{
try {
const [rows] = await pool.query(getMemberQuery, [memberId]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const getMemberByCode = async (memberCode) => {
const getMemberQuery="SELECT * FROM `members` WHERE `member_code`=?";
try{
try {
const [rows] = await pool.query(getMemberQuery, [memberCode]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const getMemberByNickname = async (memberNickname) => {
const getMemberQuery="SELECT * FROM `members` WHERE `member_nickname`=?";
try{
try {
const [rows] = await pool.query(getMemberQuery, [memberNickname]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const getMember = async (studentEnrolled, studentGrade, studentClass, studentNo, studentName) => {
const getMemberQuery="SELECT * FROM `members` WHERE `member_enrolled`=? AND `member_grade`=? AND `member_class`=? AND `member_studentNo`=? AND `member_name`=?";
try{
try {
const [rows] = await pool.query(getMemberQuery, [studentEnrolled, studentGrade, studentClass, studentNo, studentName]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const getMemberFromCode = async (studentEnrolled, studentGrade, studentClass, studentNo, studentName) => {
const getMemberQuery="SELECT * FROM `student` WHERE `member_enrolled`=? AND `member_grade`=? AND `member_class`=? AND `member_studentNo`=? AND `member_name`=?";
try{
try {
const [rows] = await pool.query(getMemberQuery, [studentEnrolled, studentGrade, studentClass, studentNo, studentName]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const getStudentInfoByCode = async (code) => {
const getCodeQuery="SELECT * FROM `student` WHERE `code`=?"
try{
try {
const [rows] = await pool.query(getCodeQuery, [code]);
if(rows.length)
if (rows.length)
return rows[0];
else
return null;
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
}

const updateCodeAvailable = (code, flag) => {
const updateCodeAvailableQuery="UPDATE `student` SET `code_available`=? WHERE `code`=?";
try{
try {
pool.query(updateCodeAvailableQuery, [flag, code]);
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
Expand Down Expand Up @@ -127,9 +127,9 @@ const signUp = async (
email,
uniqNo
];
try{
try {
await pool.query(signUpQuery, params);
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
Expand All @@ -141,9 +141,9 @@ const updatePWByCode = async (memberCode, memberPw) => {
salt=crypto.randomBytes(32).toString('hex');
memberPw=crypto.createHash('sha3-256').update(salt+memberPw).digest('hex');
const pwEditQuery="UPDATE `members` SET `member_pw`=?, `member_salt`=? WHERE `member_code`=?";
try{
try {
await pool.query(pwEditQuery, [memberPw, salt, memberCode]);
}catch(err){
} catch(err) {
console.error(err);
throw new InternalServerException();
}
Expand Down
Loading

0 comments on commit b06b8e6

Please sign in to comment.