-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.js
58 lines (52 loc) · 1.35 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const express = require('express')
const router = express.Router()
const Book = require('./bookSchema')
const User = require('./userSchema')
const mongoose = require('mongoose')
const bcrypt = require('bcrypt');
const saltRounds = 10;
var logon = false
router.get("/login", (req, res)=>{
logon = false;
res.render('userlog')
})
router.post("/db",async (req,res)=>{
const check = req.body.pass
const uname = req.body.uname
const data = await User.findOne({username: uname})
console.log(data)
if(data !== null) {
const hash = data.password
console.log(req.body)
console.log(hash)
bcrypt.compare(check, hash, (err, result) => {
console.log(result)
if(result == true) {
logon = true
res.render('querypage')
}
else {
res.render('userlog')
}
})
}
else res.render('userlog')
})
router.get("/all", checkLog, async (req,res)=>{
const result = await Book.find()
res.send(result)
})
router.get("/find", checkLog, async (req, res)=>{
const id = req.query.qid
const result = await Book.find({id: parseInt(id)})
res.send(result)
})
function checkLog(req, res, next) {
if(logon) {
next()
}
else {
res.redirect("/user/login")
}
}
module.exports = router