From e62e0812d0c0b4fa5251c805d2c7d73b77ff7763 Mon Sep 17 00:00:00 2001 From: 0standing2 Date: Wed, 18 Aug 2021 11:42:09 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=20=EB=B0=8F=20=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 7 -- controllers/auth.ctrl.js | 184 ++++++++++++++++++++++++++++++++ controllers/dailynote.ctrl.js | 89 ++++++++-------- controllers/dday.ctrl.js | 1 - controllers/manage.ctrl.js | 1 - controllers/mypage.ctrl.js | 3 - controllers/page.ctrl.js | 1 - controllers/timerDay.ctrl.js | 1 - controllers/timerWeek.ctrl.js | 1 - controllers/todo.ctrl.js | 132 +++++++++++++++++++++++ routes/auth.js | 191 ++-------------------------------- routes/middlewares.js | 6 +- routes/mypage.js | 7 +- routes/page.js | 2 +- routes/timerDay.js | 4 +- routes/timerWeek.js | 5 +- routes/todo.js | 145 ++------------------------ 17 files changed, 382 insertions(+), 398 deletions(-) diff --git a/app.js b/app.js index 5cadbc0..5fc93c4 100644 --- a/app.js +++ b/app.js @@ -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)); diff --git a/controllers/auth.ctrl.js b/controllers/auth.ctrl.js index e69de29..5fcac0b 100644 --- a/controllers/auth.ctrl.js +++ b/controllers/auth.ctrl.js @@ -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 +} \ No newline at end of file diff --git a/controllers/dailynote.ctrl.js b/controllers/dailynote.ctrl.js index 39210aa..574966b 100644 --- a/controllers/dailynote.ctrl.js +++ b/controllers/dailynote.ctrl.js @@ -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 추가 오류' }); } }; diff --git a/controllers/dday.ctrl.js b/controllers/dday.ctrl.js index 795b362..91a4f7a 100644 --- a/controllers/dday.ctrl.js +++ b/controllers/dday.ctrl.js @@ -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; diff --git a/controllers/manage.ctrl.js b/controllers/manage.ctrl.js index beb1363..2ddbb55 100644 --- a/controllers/manage.ctrl.js +++ b/controllers/manage.ctrl.js @@ -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; diff --git a/controllers/mypage.ctrl.js b/controllers/mypage.ctrl.js index 3aa2aab..b8ec205 100644 --- a/controllers/mypage.ctrl.js +++ b/controllers/mypage.ctrl.js @@ -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; diff --git a/controllers/page.ctrl.js b/controllers/page.ctrl.js index 802bded..6efb949 100644 --- a/controllers/page.ctrl.js +++ b/controllers/page.ctrl.js @@ -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 join = (req, res) => { console.log('회원가입으로 이동'); diff --git a/controllers/timerDay.ctrl.js b/controllers/timerDay.ctrl.js index f9d0e0d..7d6b506 100644 --- a/controllers/timerDay.ctrl.js +++ b/controllers/timerDay.ctrl.js @@ -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; diff --git a/controllers/timerWeek.ctrl.js b/controllers/timerWeek.ctrl.js index 527e53c..ec39133 100644 --- a/controllers/timerWeek.ctrl.js +++ b/controllers/timerWeek.ctrl.js @@ -1,7 +1,6 @@ const express = require('express'); const db = require('../models/db')(); const connection = db.init(); -db.test_open(connection); const rank = (req,res)=>{ try{ diff --git a/controllers/todo.ctrl.js b/controllers/todo.ctrl.js index e69de29..1365088 100644 --- a/controllers/todo.ctrl.js +++ b/controllers/todo.ctrl.js @@ -0,0 +1,132 @@ +const express = require('express'); +const moment = require('moment'); +const solux_db = require('../models/db')(); +const connection = solux_db.init(); + +const list = (req,res)=>{ + const todoid = req.decoded.id; + try{ + console.log("todoid: "+todoid); + const searchList = "SELECT * from todo WHERE todoid = ?"; + connection.query(searchList, todoid, (err,result)=>{ + console.log(result); + if (result.length==0){ + return res.json({success : true, message : '아직 todo 없음'}); + } + return res.send(result); + }) + }catch(err){ + console.error(err); + return res.json({success : false, message : 'todo list 오류'}); + } +}; + +const change = (req,res)=>{ + const index =req.body.index; + const content = req.body.content; + try{ + const sqlCheck = "UPDATE todo SET content = ? WHERE `index` = ?"; + connection.query(sqlCheck, [content, index] , (err, result)=>{ + console.log(result); + if(err){ + console.log(err); + return res.json({success : false, message : 'todo change 오류'}); + }else{ + console.log('change 성공'); + return res.redirect('/todo/list'); + } + }) + }catch(err){ + console.log(err); + } +}; + +const check = (req,res)=>{ + const index =req.body.index; + try{ + const sqlSearch = "SELECT `check` from todo WHERE `index`=?"; + connection.query(sqlSearch, index, (err,result)=>{ + console.log(result[0].check); + const ck = result[0].check; + console.log(ck); + + const sqlCheck = "UPDATE todo SET `check` = ? WHERE `index` = ?"; + if (ck==1){ + connection.query(sqlCheck, [0, index] , (err, result2)=>{ + console.log(result2); + if(err){ + console.log(err); + return res.json({success : false, message : 'todo check to 0 오류'}); + }else{ + console.log('update 성공'); + return res.redirect('/todo/list'); + } + }) + }else{ + connection.query(sqlCheck, [1, index ], (err, result2)=>{ + console.log(result2); + if(err){ + console.log(err); + return res.json({success : false, message : 'todo check to 1 오류'}); + }else{ + console.log('update 성공'); + return res.redirect('/todo/list'); + } + }) + } + }) + }catch(err){ + console.log(err); + } +}; + +const del = (req, res)=>{ + console.log('delete 실행 시작'); + const index = req.body.index; + try{ + const sqlDelete = "DELETE FROM todo WHERE `index` = ?"; + connection.query(sqlDelete, index, (err, result)=>{ + console.log(result); + if(err){ + console.log(err); + return res.json({success : false, message : 'todo delete 오류'}); + } + else{ + console.log("Number of records deleted: " + result.affectedRows); + return res.redirect('/todo/list'); + } + }); + }catch(err){ + console.log(err); + } +}; + +const insert = (req,res)=>{ + const todoid = req.decoded.id; + const content = req.body.content; + try{ + console.log(todoid); + console.log(content); + console.log(moment().format("YYYY-MM-DD")); + const insertTodo = 'INSERT INTO todo (todoid, content, date, `check`) VALUES(?,?,?,?)'; + connection.query(insertTodo, [todoid, content, moment().format("YYYY-MM-DD"), 0 ] , async(err,result)=>{ + if(err) console.log(err); + else{ + console.log('todo 추가 성공'); + return res.redirect('/todo/list'); + } + }); + }catch(err){ + console.log(err); + return res.json({success : false, message : 'todo 추가 오류'}); + } + +}; + +module.exports = { + list, + change, + check, + del, + insert +} \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js index dc3be6f..729b008 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -1,194 +1,19 @@ const express = require('express'); -const bcrypt = require('bcrypt'); -const passport = require('passport'); -const jwt = require('jsonwebtoken'); -//const pageRouter = require('./page'); -const mysql = require('mysql'); -const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); -const db = require('../models/db'); -const path = require('path'); - +const { isLoggedIn, isNotLoggedIn } = require('./middlewares'); +const ctrl = require("../controllers/auth.ctrl"); const router = express.Router(); -const solux_db = require('../models/db')(); -const connection = solux_db.init(); - -solux_db.test_open(connection); - -router.post('/join', isNotLoggedIn, 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); - } -}); - -router.post('/login', isNotLoggedIn, (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); - } - //req.session.userid = user.id; - //console.log(req.session); - 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 : "로그인 성공"}); - //return res.json({ result : 'ok', token }); - }); - })(req, res, next); // 미들웨어 내의 미들웨어에는 (req, res, next)를 붙입니다. - }); - +router.post('/join', isNotLoggedIn, ctrl.join); -router.get('/logout', isLoggedIn, (req,res)=>{ - console.log('로그아웃'); - //req.cookies.set('loginToken'); - //res.status=240; - res.clearCookie("loginToken"); - req.logout(); - req.session.destroy(); - return res.status(205).json({ success : true, message : "로그아웃 성공"}); -}); +router.post('/login', isNotLoggedIn, ctrl.login); -router.post('/searchId', isNotLoggedIn, (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); - } -}) +router.get('/logout', isLoggedIn, ctrl.logout); -router.post('/searchPw', isNotLoggedIn, (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); - } -}) +router.post('/searchId', isNotLoggedIn, ctrl.searchId); -router.get('/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); - } -}) +router.post('/searchPw', isNotLoggedIn, ctrl.searchPw); +router.get('/checkId', ctrl.checkId); module.exports = router; \ No newline at end of file diff --git a/routes/middlewares.js b/routes/middlewares.js index 386d24f..8c3dedc 100644 --- a/routes/middlewares.js +++ b/routes/middlewares.js @@ -2,7 +2,6 @@ const jwt = require('jsonwebtoken'); exports.isLoggedIn = (req, res, next) => { if(req.cookies.loginToken) { - //if (req.isAuthenticated()) { next(); } else { res.status(403).send('로그인 필요'); @@ -11,18 +10,15 @@ exports.isLoggedIn = (req, res, next) => { exports.isNotLoggedIn = (req, res, next) => { if(!req.cookies.loginToken){ - //if (!req.isAuthenticated()) { next(); } else { - const message = encodeURIComponent('로그인한 상태입니다.'); - res.redirect(`/?error=${message}`); + res.json({success : false, message : '로그인한 상태입니다.'}); } }; exports.verifyToken = (req,res,next)=>{ try{ const token = req.cookies.loginToken; - //const token = req.headers.authorization.split('Bearer')[1]; req.decoded = jwt.verify(token,process.env.JWT_SECRET); return next(); diff --git a/routes/mypage.js b/routes/mypage.js index 9943b12..430fe1a 100644 --- a/routes/mypage.js +++ b/routes/mypage.js @@ -1,11 +1,6 @@ const express = require('express'); -const jsonwebtoken = require('jsonwebtoken'); -const bcrypt = require('bcrypt'); -const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); +const { verifyToken } = require('./middlewares'); const router = express.Router(); -const db = require('../models/db')(); -const connection = db.init(); -db.test_open(connection); const ctrl = require("../controllers/mypage.ctrl"); router.get('/info', verifyToken, ctrl.info); diff --git a/routes/page.js b/routes/page.js index 16ebabf..8044bef 100644 --- a/routes/page.js +++ b/routes/page.js @@ -1,5 +1,5 @@ const express = require('express'); -const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); +const { isNotLoggedIn, verifyToken } = require('./middlewares'); const router = express.Router(); const ctrl = require("../controllers/page.ctrl"); diff --git a/routes/timerDay.js b/routes/timerDay.js index efdc061..210e6bd 100644 --- a/routes/timerDay.js +++ b/routes/timerDay.js @@ -3,11 +3,13 @@ const router = express.Router(); const { verifyToken } = require('./middlewares'); const schedule = require('node-schedule'); const ctrl = require("../controllers/timerDay.ctrl"); +const db = require('../models/db')(); +const connection = db.init(); router.get('/list', verifyToken, ctrl.list); router.post('/update', verifyToken, ctrl.update); -schedule.scheduleJob('0 0 0 * * *', async () => { +schedule.scheduleJob('0 37 11 * * *', async () => { try { //const timerDayid = req.decoded.id; //현재 로그인한 사용자 뿐만 아니라 전체 사용자의 기록 갱신 (수정) const sqlSelectWeek = "SELECT recordWeek from timerWeek WHERE timerWeekid = ?"; diff --git a/routes/timerWeek.js b/routes/timerWeek.js index fb23122..797fb94 100644 --- a/routes/timerWeek.js +++ b/routes/timerWeek.js @@ -1,10 +1,9 @@ const express = require('express'); const schedule = require('node-schedule'); -const db = require('../models/db')(); -const connection = db.init(); const router = express.Router(); -db.test_open(connection); const ctrl = require("../controllers/timerWeek.ctrl"); +const db = require('../models/db')(); +const connection = db.init(); router.get('/rank', ctrl.rank); diff --git a/routes/todo.js b/routes/todo.js index 79cb9c8..2d5e93a 100644 --- a/routes/todo.js +++ b/routes/todo.js @@ -1,145 +1,12 @@ const express = require('express'); -const mysql = require('mysql'); -const moment = require('moment'); - - const router = express.Router(); - -const path = require('path'); const { verifyToken } = require('./middlewares'); +const ctrl = require('../controllers/todo.ctrl'); -const solux_db = require('../models/db')(); -const connection = solux_db.init(); - -solux_db.test_open(connection); - -router.get('/list', verifyToken, (req,res)=>{ - //const todoid = req.body.todoid; - //const token = req.body.token; - //const todoid = 'ex2'; - const todoid = req.decoded.id; - try{ - console.log("todoid: "+todoid); - //console.log("req.cookies: "+req.cookies); - const searchList = "SELECT * from todo WHERE todoid = ?"; - connection.query(searchList, todoid, (err,result)=>{ - console.log(result); - if (result.length==0){ - return res.json({success : true, message : '아직 todo 없음'}); - } - return res.send(result); - }) - }catch(err){ - console.error(err); - return res.json({success : false, message : 'todo list 오류'}); - } -}); - -router.post('/change', (req,res)=>{ - const index =req.body.index; - const content = req.body.content; - try{ - const sqlCheck = "UPDATE todo SET content = ? WHERE `index` = ?"; - connection.query(sqlCheck, [content, index] , (err, result)=>{ - console.log(result); - if(err){ - console.log(err); - return res.json({success : false, message : 'todo change 오류'}); - }else{ - console.log('change 성공'); - return res.redirect('/todo/list'); - } - }) - }catch(err){ - console.log(err); - } -}); - -router.post('/check', (req,res)=>{ - const index =req.body.index; - try{ - const sqlSearch = "SELECT `check` from todo WHERE `index`=?"; - connection.query(sqlSearch, index, (err,result)=>{ - console.log(result[0].check); - const ck = result[0].check; - console.log(ck); - - const sqlCheck = "UPDATE todo SET `check` = ? WHERE `index` = ?"; - if (ck==1){ - connection.query(sqlCheck, [0, index] , (err, result2)=>{ - console.log(result2); - if(err){ - console.log(err); - return res.json({success : false, message : 'todo check to 0 오류'}); - }else{ - console.log('update 성공'); - return res.redirect('/todo/list'); - } - }) - }else{ - connection.query(sqlCheck, [1, index ], (err, result2)=>{ - console.log(result2); - if(err){ - console.log(err); - return res.json({success : false, message : 'todo check to 1 오류'}); - }else{ - console.log('update 성공'); - return res.redirect('/todo/list'); - } - }) - } - }) - - - }catch(err){ - console.log(err); - } -}); - -router.post('/delete', (req, res)=>{ - console.log('delete 실행 시작'); - const index = req.body.index; - try{ - const sqlDelete = "DELETE FROM todo WHERE `index` = ?"; - connection.query(sqlDelete, index, (err, result)=>{ - console.log(result); - if(err){ - console.log(err); - return res.json({success : false, message : 'todo delete 오류'}); - } - else{ - console.log("Number of records deleted: " + result.affectedRows); - return res.redirect('/todo/list'); - } - }); - }catch(err){ - console.log(err); - } -} -); - - -router.post('/insert', verifyToken, (req,res)=>{ - const todoid = req.decoded.id; - const content = req.body.content; - try{ - console.log(todoid); - console.log(content); - console.log(moment().format("YYYY-MM-DD")); - const insertTodo = 'INSERT INTO todo (todoid, content, date, `check`) VALUES(?,?,?,?)'; - connection.query(insertTodo, [todoid, content, moment().format("YYYY-MM-DD"), 0 ] , async(err,result)=>{ - if(err) console.log(err); - else{ - console.log('todo 추가 성공'); - return res.redirect('/todo/list'); - //return res.send(content); - } - }); - }catch(err){ - console.log(err); - return res.json({success : false, message : 'todo 추가 오류'}); - } - -}); +router.get('/list', verifyToken, ctrl.list); +router.post('/change', ctrl.change); +router.post('/check', ctrl.check); +router.post('/delete', ctrl.del); +router.post('/insert', verifyToken, ctrl.insert); module.exports = router; \ No newline at end of file