Commit d910be68 authored by Kim, Subin's avatar Kim, Subin
Browse files

Merge remote-tracking branch 'origin/rkyoung7' into kimpen

parents ae7f2411 662b4c70
This diff is collapsed.
...@@ -10,11 +10,11 @@ ...@@ -10,11 +10,11 @@
"cookie-parser": "~1.4.4", "cookie-parser": "~1.4.4",
"debug": "~2.6.9", "debug": "~2.6.9",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "~4.16.1", "express": "^4.17.1",
"http-errors": "~1.6.3", "http-errors": "^1.6.3",
"jsonwebtoken": "^8.5.1", "jsonwebtoken": "^8.5.1",
"mongoose": "^5.10.5", "mongoose": "^5.10.6",
"morgan": "~1.9.1", "morgan": "~1.9.1",
"pug": "2.0.0-beta11" "pug": "^3.0.0"
} }
} }
var express = require('express'); const express = require('express');
var router = express.Router(); const User = require('../schemas/user');
const router = express.Router();
/* GET home page. */ /* GET home page. */
router.get('/', function(req, res, next) { // router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' }); // res.render('index', { title: 'Express' });
}); // });
module.exports = router; module.exports = router;
const express = require('express');
const User = require('../schemas/user');
const bcrypt = require("bcrypt");
const jwt = require('jsonwebtoken');
const user = require('../schemas/user');
const router = express.Router();
router.get('/', function (req, res, next) {
console.log('/login get request', req.body)
User.find({})
.then((signups) => {
res.json(signups);
})
.catch((err) => {
console.error(err);
next(err);
});
});
router.post('/', function (req, res, next) {
console.log('/login post request', req.body)
User.findOne({ id: req.body.id }, 'id password name', function (err, users) {
if (err) return res.status(500).json({ error: err });
if (!users) {
return res.status(404).json({ error: '해당 학번이 존재하지 않습니다.' });
}
bcrypt.compare(req.body.password, users.password, function (err, result) {
if (err) {
console.log(err)
return res.status(500).json({ error: err });
}
if (result) {
const token = jwt.sign({
id: users.id,
}, process.env.JWT_SECRET, {
expiresIn: '1m',
});
return res.status(201).json({
token,
users,
});
}
res.status(404).json({ error: '비밀번호를 다시 입력해주세요.' });
});
})
});
router.post('/find', function (req, res, next) {
console.log('/find post request', req.body)
User.findOne({ id: req.body.id }, 'id question answer', function (err, users) {
if (err) return res.status(500).json({ error: err });
if (!users) return res.status(404).json({ error: '해당 학번이 존재하지 않습니다.' });
if (users.question === req.body.question) {
if (users.answer === req.body.answer) {
return res.status(201).json({users});
}
return res.status(404).json({ error: '답변이 일치하지 않습니다.'});
}
return res.status(404).json({error: '질문을 다시 선택해주세요.'});
})
});
module.exports = router;
const jwt = require('jsonwebtoken');
exports.isLoggedIn = (req, res, next) => {
if (req.isAuthenticated()) {
next();
} else {
res.status(403).send('로그인 필요');
}
};
exports.isNotLoggedIn = (req, res, next) => {
if (!req.isAuthenticated()) {
next();
} else {
res.redirect('/');
}
};
exports.verifyToken = (req, res, next) => {
try {
req.decoded = jwt.verify(req.headers.authorization, process.env.JWT_SECRET);
console.log('decode', req.decoded)
return next();
} catch (error) {
if (error.name === 'TokenExpiredError') { // 유효기간 초과
return res.status(419).json({
code: 419,
error: '토큰이 만료되었습니다. 다시 로그인 해주세요.',
});
}
return res.status(401).json({
code: 401,
error: '유효하지 않은 토큰입니다. 다시 로그인 해주세요.',
});
}
};
const express = require('express');
const Notice = require('../schemas/notice');
const router = express.Router();
router.get('/', function (req, res, next) {
Notice.find({}).sort({ post_date: -1 })
.then((notices) => {
res.json(notices);
})
.catch((err) => {
console.error(err);
next(err);
});
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const User = require('../schemas/user');
const Reserve = require('../schemas/reserve');
const { verifyToken } = require('./middlewares');
const router = express.Router();
router.post('/', function (req, res, next) {
console.log('/reserve post req.body', req.body)
const reserve = new Reserve({
user: req.body._id,
date: req.body.date,
starttime: Number(req.body.starttime),
usetime: Number(req.body.usetime),
start: `${req.body.date}T`+`${req.body.starttime}:00:00`,
end: `${req.body.date}T`+`${Number(req.body.starttime)+Number(req.body.usetime)}:00:00`,
room: req.body.room,
reason: req.body.reason,
students: req.body.students,
approve: req.body.approve,
num: req.body.students.length + 1,
});
reserve.save()
.then((result) => {
console.log(result);
res.status(201).json(result);
})
.catch((err) => {
console.error(err);
next(err);
});
});
// router.get('/:_id', verifyToken, function (req, res, next) {
router.get('/:_id', function (req, res, next) {
console.log('/reserves get req.params', req.params)
Reserve.find({ user: req.params._id }, function (err, reserve) {
console.log('id.name',reserve)
if (err) return res.status(500).json({ error: err });
console.log('reserve list', reserve)
res.status(201).json(reserve);
})
});
router.get('/admin/:_id', function (req, res, next) {
console.log('/reserves/admin get req.params', req.params)
Reserve.find({ approve: false }).populate('user').exec(function (err, reserve) {
if (err) return res.status(500).json({ error: err });
console.log('reserve list', reserve)
res.status(201).json(reserve);
})
});
router.delete('/:_id', function (req, res, next) {
console.log('/reserves delete req.params', req.params)
Reserve.findOne({ _id: req.params._id }, function (err, reserve) {
if (err) return res.status(500).json({ error: err });
reserve.remove()
.then(() => {
console.log();
res.status(201).json();
})
.catch((err) => {
console.error(err);
next(err);
});
})
});
router.put('/:id', function (req, res, next) {
console.log('/reserves put req.body', req.params)
Reserve.findOne({ _id: req.params.id }, 'approve', function (err, reserve) {
if (err) return res.status(500).json({ error: err });
reserve.approve = true;
reserve.save()
.then((result) => {
console.log(result);
res.status(201).json(result);
})
.catch((err) => {
console.error(err);
next(err);
});
});
});
module.exports = router;
var express = require('express'); const express = require('express');
var router = express.Router(); const User = require('../schemas/user');
const bcrypt = require("bcrypt");
const { verifyToken } = require('./middlewares');
/* GET users listing. */ const router = express.Router();
router.get('/', function(req, res, next) {
res.send('respond with a resource'); router.post('/', function (req, res, next) {
console.log('/users post req.body', req.body)
User.findOne({ id: req.body.id }, function (err, users) {
if (err) return res.status(500).json({ error: err });
if (users) {
return res.status(404).json({ error: '이미 존재하는 학번입니다.' })
};
const user = new User({
name: req.body.name,
id: req.body.id,
password: req.body.password,
question: req.body.question,
answer: req.body.answer,
});
user.save()
.then((result) => {
console.log(result);
res.status(201).json(result);
})
.catch((err) => {
console.error(err);
next(err);
});
})
});
router.get('/:_id', function (req, res, next) {
console.log('/users get req.params', req.params)
User.findOne({ _id: req.params._id }, function (err, user) {
if (err) return res.status(500).json({ error: err });
res.status(201).json(user);
})
});
router.get('/admin/:_id', function (req, res, next) {
console.log('/admin get req.params', req.params)
User.findOne({ _id: req.params._id }, 'role', function (err, user) {
if (err) return res.status(500).json({ error: err });
if (user.role == 'admin') {
next();
}
else return res.status(404).json({ error: '권한이 없습니다.' })
})
});
router.put('/change/:id', function (req, res, next) {
console.log('/change put req.body', req.params)
User.findOne({ _id: req.params.id }, 'password', function (err, user) {
if (err) return res.status(500).json({ error: err });
bcrypt.compare(req.body.password, user.password, function (err, result) {
if (err) {
console.log(err)
return res.status(500).json({ error: err });
}
if (result) {
return res.status(404).json({ error: '새로운 비밀번호를 입력해주세요.' })
}
});
user.password = req.body.password;
user.save()
.then((result) => {
console.log(result);
res.status(201).json(result);
})
.catch((err) => {
console.error(err);
next(err);
});
})
}); });
module.exports = router; module.exports = router;
...@@ -5,8 +5,8 @@ module.exports = () => { ...@@ -5,8 +5,8 @@ module.exports = () => {
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true); mongoose.set('debug', true);
} }
mongoose.connect('mongodb://hijinju:highlight@localhost:27017/admin', { mongoose.connect('mongodb://kimpen:kim65811025@localhost:27017/admin', {
dbName: 'ku_rental', dbName: 'ku_rental', useNewUrlParser: true, useUnifiedTopology: true,
}, (error) => { }, (error) => {
if (error) { if (error) {
console.log('몽고디비 연결 에러', error); console.log('몽고디비 연결 에러', error);
......
const mongoose = require('mongoose');
const { Schema } = mongoose;
const noticeSchema = new Schema({
notice_img: {
type: Array,
},
notice_title: {
type: String,
required: true,
},
notice_author: {
type: String,
// default: "나야나"
required: true,
},
post_date: {
type: Date,
default: Date.now,
required: true,
},
notice_content: {
type: String,
required: true,
},
});
module.exports = mongoose.model('Notice', noticeSchema);
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const { Schema } = mongoose; const { Schema } = mongoose;
const { Types: { ObjectId } } = Schema;
const reserveSchema = new Schema({ const reserveSchema = new Schema({
user: {
type: ObjectId,
required: true,
ref: 'User',
},
date: { date: {
type: String, type: String,
}, },
time: { starttime: {
type: String, type: Number,
}, },
room:{ usetime: {
type: Number,
},
start: {
type: String, type: String,
}, },
name: { //대표자 이름 end: {
type: String, type: String,
}, },
id:{ room: {
type: Number, type: String,
}, },
reason: { reason: {
type: String, type: String,
}, },
member: { students: {
type: String, type: Array,
}, },
approve: { approve: {
type: Boolean, type: Boolean,
default: false,
}, },
num: { num: {
type: Number, type: Number,
} },
}); });
module.exports = mongoose.model('Reserve', reserveSchema); module.exports = mongoose.model('Reserve', reserveSchema);
\ No newline at end of file
...@@ -12,6 +12,17 @@ const userSchema = new Schema({ ...@@ -12,6 +12,17 @@ const userSchema = new Schema({
type: String, type: String,
}, },
role: {
type: String,
default:'user',
},
answer:{
type: String,
},
question: {
type: String,
},
id: { id: {
type: Number, type: Number,
required: true, required: true,
...@@ -35,7 +46,7 @@ userSchema.pre("save", function (next) { ...@@ -35,7 +46,7 @@ userSchema.pre("save", function (next) {
} else { } else {
next(); next();
} }
}); });
module.exports = mongoose.model('User', userSchema); module.exports = mongoose.model('User', userSchema);
\ No newline at end of file
extends layout
block content
h1= message
h2= error.status
pre #{error.stack}
extends layout
block content
h1= title
p Welcome to #{title}
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment