Skip to content

Commit

Permalink
Merge pull request #25 from SoluxProject/feature/#24
Browse files Browse the repository at this point in the history
컨트롤러 분리 및 코드 정리
  • Loading branch information
0standing2 authored Aug 18, 2021
2 parents baa355f + e62e081 commit 8fd8bd8
Show file tree
Hide file tree
Showing 17 changed files with 382 additions and 398 deletions.
7 changes: 0 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ app.set('port', process.env.PORT || 3001);
app.set("etag",false);
const options = { etag : false };
passportConfig();
/*
app.set('view engine', 'html');

nunjucks.configure('views', {
express: app,
watch: true,
});
*/
app.use(morgan('dev'));
//app.use(express.static(path.join(__dirname, 'public'),options));
app.use(express.static(path.join(__dirname, '../client/build/'),options));
Expand Down
184 changes: 184 additions & 0 deletions controllers/auth.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
const express = require('express');
const bcrypt = require('bcrypt');
const passport = require('passport');
const jwt = require('jsonwebtoken');
const solux_db = require('../models/db')();
const connection = solux_db.init();
solux_db.test_open(connection);

const join = async (req, res, next) => {
const { id, pw, email, major, name, tel } = req.body;
try {
const sqlSearch = "SELECT * from user where id=?";

connection.query(sqlSearch, id, async (err, result) => {
if (err) {
console.log(err);
return res.json({ success: false, message: "회원가입 실패" })
}
else {
console.log(result);
if (result.length != 0) { //해당 id가 존재할때
console.log('이미 존재하는 id');
return res.json({ success: false, message: "이미 존재하는 id 입니다." })
}
const hash = await bcrypt.hash(pw, 12);
const sqlInsert = "INSERT INTO user (id,pw,email,major,name,tel) VALUES (?,?,?,?,?,?)"
connection.query(sqlInsert, [id, hash, email, major, name, tel], (err, result) => {
if (err) console.log(err);
else {
console.log('회원가입 성공 후 이동');
const timerDayInsert = "INSERT INTO timerDay (timerDayid) VALUES (?)";
connection.query(timerDayInsert, [id], (err, result) => {
if (err) console.log(err);
else {
console.log('timerDay 추가');
const timerWeekInsert = "INSERT INTO timerWeek (timerWeekid) VALUES (?)";
connection.query(timerWeekInsert, [id], (err, result) => {
if (err) console.log(err);
else {
console.log('timerWeek 추가');
return res.json({ success: true, message: "회원가입 성공" });
}
})
}
})
}
});
}
});

} catch (err) {
console.log(err);
return next(err);
}
};

const login = (req, res, next) => {
passport.authenticate('local', { session: false }, (authError, user, info) => {
if (authError) {//서버 에러의 경우
console.error(authError);
return next(authError);
}
if (!user) { //로그인 실패
console.log('회원이 아닙니다');
return res.json({ success: false, message: info.message });
}
return req.login(user, { session: false }, (loginError) => {//로그인 성공
if (loginError) {//index의 serializeUser 성공 후 실행
console.error(loginError);
return next(loginError);
}
const token = jwt.sign(
{ id: user.id }, process.env.JWT_SECRET, { expiresIn: "1m" });
console.log('로그인 성공');
console.log(user.id);
//세션 쿠키를 브라우저로 보내줘요.
//return res.json({success : true, message : "로그인 성공", token});
console.log(token);
res.cookie("loginToken", token, { maxAge: 60000 });
return res.json({ success: true, message: "로그인 성공" });
});
})(req, res, next); // 미들웨어 내의 미들웨어에는 (req, res, next)를 붙입니다.
};

const logout = (req, res) => {
console.log('로그아웃');
res.clearCookie("loginToken");
req.logout();
req.session.destroy();
return res.status(205).json({ success: true, message: "로그아웃 성공" });
};

const searchId = (req, res) => {
const { name, tel } = req.body;
try {
console.log(name + "," + tel);
const sqlSearch = "SELECT id from user WHERE name = ? AND tel = ?";
connection.query(sqlSearch, [name, tel], (err, result) => {
if (err) {
console.log(err);
}
else {
if (result.length == 0) {
console.log('존재하지 않는 회원');
return res.json({ result: false, message: '존재하지 않는 회원입니다.' });
}
else {
console.log('ID 찾기 성공');
return res.json({ result: true, message: result });
}
}
})
} catch (err) {
console.log(err);
}
};

const searchPw = (req, res) => {
const { id, name, newPw } = req.body;
try {
console.log(id + "," + name);
const sqlSearch = "SELECT * from user WHERE id = ? AND name = ?";
connection.query(sqlSearch, [id, name], async (err, result) => {
if (err) {
console.log(err);
}
else {
if (result.length == 0) {
console.log('존재하지 않는 회원');
return res.json({ result: false, message: '존재하지 않는 회원입니다.' });
}
else {
const pwUpdate = "UPDATE user SET pw = ? WHERE id=? AND name = ?";
const hash = await bcrypt.hash(newPw, 12);
connection.query(pwUpdate, [hash, id, name], async (err, result) => {
if (err) {
console.log(err);
return res.json({ result: false, message: '비밀번호 변경 오류입니다.' });
}
else {
console.log('pw변경 성공');
return res.json({ result: true, message: '비밀번호 변경 완료' });
}
})
}
}
});
} catch (err) {
console.log(err);
}
};

const checkId = (req, res) => {
const id = req.body.id;
try {
console.log(id);
const checkId = "SELECT * from user WHERE id=?";
connection.query(checkId, id, async (err, result) => {
if (err) {
console.log(err);
return res.json({ success: false, message: 'ID 중복확인 오류' });
}
if (result.length != 0) {
console.log('이미 사용중인 ID');
return res.json({ success: false, message: '이미 사용중인 ID 입니다.' });
}
else {
console.log('사용가능한 ID');
return res.json({ success: true, message: '사용가능한 ID 입니다.' });
}
})
} catch (err) {
console.log(err);
}
};

module.exports = {
join,
login,
logout,
searchId,
searchPw,
checkId
}
89 changes: 44 additions & 45 deletions controllers/dailynote.ctrl.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,125 @@
const express = require('express');
const solux_db = require('../models/db')();
const connection = solux_db.init();
solux_db.test_open(connection);

const list = (req,res)=>{
const list = (req, res) => {
const dailyid = req.decoded.id;
try{
console.log("dailyid: "+dailyid);
try {
console.log("dailyid: " + dailyid);
//console.log("req.cookies: "+req.cookies);
const searchList = "SELECT * from daily WHERE dailyid = ?";
connection.query(searchList, dailyid, (err,result)=>{
connection.query(searchList, dailyid, (err, result) => {
console.log(result);
if (result.length==0){
return res.json({success : true, message : '아직 dailynote 없음'});
if (result.length == 0) {
return res.json({ success: true, message: '아직 dailynote 없음' });
}
return res.send(result);
})
}catch(err){
} catch (err) {
console.error(err);
return err;
}
};

const change = (req,res)=>{
const index =req.body.index;
const change = (req, res) => {
const index = req.body.index;
const content = req.body.content;
try{
try {
const sqlCheck = "UPDATE daily SET content = ? WHERE `index` = ?";
connection.query(sqlCheck, [content, index] , (err, result)=>{
connection.query(sqlCheck, [content, index], (err, result) => {
console.log(result);
if(err){
if (err) {
console.log(err);
return res.json({success : false, message : 'daily change 오류'});
}else{
return res.json({ success: false, message: 'daily change 오류' });
} else {
console.log('daily 완료');
return res.redirect('/dailynote/list');
}
})
}catch(err){
} catch (err) {
console.log(err);
}
};

const check = (req,res)=>{
const index =req.body.index;
try{
const check = (req, res) => {
const index = req.body.index;
try {
const sqlSearch = "SELECT `check` from daily WHERE `index`=?";
connection.query(sqlSearch, index, (err,result)=>{
connection.query(sqlSearch, index, (err, result) => {
console.log(result[0].check);
const ck = result[0].check;
console.log(ck);

const sqlCheck = "UPDATE daily SET `check` = ? WHERE `index` = ?";
if (ck==1){
connection.query(sqlCheck, [0, index] , (err, result2)=>{
if (ck == 1) {
connection.query(sqlCheck, [0, index], (err, result2) => {
console.log(result2);
if(err){
if (err) {
console.log(err);
return res.json({success : false, message : 'daily check to 0 오류'});
}else{
return res.json({ success: false, message: 'daily check to 0 오류' });
} else {
console.log('update to 0 완료');
return res.redirect('/dailynote/list');
}
})
}else{
connection.query(sqlCheck, [1, index ], (err, result2)=>{
} else {
connection.query(sqlCheck, [1, index], (err, result2) => {
console.log(result2);
if(err){
if (err) {
console.log(err);
return res.json({success : false, message : 'daily check to 1 오류'});
}else{
return res.json({ success: false, message: 'daily check to 1 오류' });
} else {
console.log('update to 1 완료');
return res.redirect('/dailynote/list');
}
})
}
})
}catch(err){
} catch (err) {
console.log(err);
}
};

const del = (req, res)=>{
const del = (req, res) => {
console.log('delete 실행 시작');
const index = req.body.index;
try{
try {
const sqlDelete = "DELETE FROM daily WHERE `index` = ?";
connection.query(sqlDelete, index, (err, result)=>{
connection.query(sqlDelete, index, (err, result) => {
console.log(result);
if(err){
if (err) {
console.log(err);
return res.json({success : false, message : 'daily delete 오류'});
return res.json({ success: false, message: 'daily delete 오류' });
}
else{
else {
console.log("Number of records deleted: " + result.affectedRows);
return res.redirect('/dailynote/list');
}
});
}catch(err){
} catch (err) {
console.log(err);
}
};

const insert = (req,res)=>{
const insert = (req, res) => {
const dailyid = req.decoded.id;
const { date, content, subject } = req.body;
try{
try {
console.log(dailyid);
console.log(content);

const insertTodo = 'INSERT INTO daily (dailyid, content, subject, date, `check`) VALUES(?,?,?,?,?)';
connection.query(insertTodo, [dailyid, content, subject, date, 0 ] , async(err,result)=>{
if(err) console.log(err);
else{
connection.query(insertTodo, [dailyid, content, subject, date, 0], async (err, result) => {
if (err) console.log(err);
else {
console.log('daily 추가 성공');
return res.redirect('/dailynote/list');
//return res.send(content);
}
});
}catch(err){
} catch (err) {
console.log(err);
return res.json({success : false, message : 'daily 추가 오류'});
return res.json({ success: false, message: 'daily 추가 오류' });
}
};

Expand Down
1 change: 0 additions & 1 deletion controllers/dday.ctrl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const express = require('express');
const solux_db = require('../models/db')();
const connection = solux_db.init();
solux_db.test_open(connection);

const list = (req,res)=>{
const id = req.decoded.id;
Expand Down
1 change: 0 additions & 1 deletion controllers/manage.ctrl.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const express = require('express');
const solux_db = require('../models/db')();
const connection = solux_db.init();
solux_db.test_open(connection);

const list = (req, res) => {
const manageid = req.decoded.id;
Expand Down
3 changes: 0 additions & 3 deletions controllers/mypage.ctrl.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const express = require('express');
//const jsonwebtoken = require('jsonwebtoken');
const bcrypt = require('bcrypt');
//const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares');
const db = require('../models/db')();
const connection = db.init();
db.test_open(connection);

const info = (req,res)=>{
const id = req.decoded.id;
Expand Down
Loading

0 comments on commit 8fd8bd8

Please sign in to comment.