-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
130 lines (115 loc) · 3.59 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require("express");
const app = express();
const path = require("path");
const usermodule = require("./models/user");
const adminmodule = require("./models/admin");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const cookieParser = require("cookie-parser");
const multer = require('multer')
const crypto = require('crypto')
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/images/uploads')
},
filename: function (req, file, cb) {
crypto.randomBytes(12, function(err, bytes){
let fn = bytes.toString("hex")+path.extname(file.originalname)
cb(null,fn)
})
}
})
const upload = multer({ storage: storage })
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.get("/", function (req, res) {
res.render("registration");
});
app.post("/admin", function (req, res) {
const { email, password } = req.body;
bcrypt.genSalt(10, function (err, salt) {
bcrypt.hash(password, salt, async function (err, hash) {
await adminmodule.create({
email,
password: hash,
});
});
});
const token = jwt.sign({ email: email, password: password }, "64bits");
res.cookie("token", token);
res.redirect("/createuser");
});
app.get("/login", function (req, res) {
res.render("login");
});
app.post("/loginadmin", async function (req, res) {
const { email, password } = req.body;
let useremail = await adminmodule.findOne({ email });
if (!useremail) res.send("something went wrong");
else {
bcrypt.compare(password, useremail.password, function (err, result) {
if (result) res.redirect("/createuser");
else res.send("something went wrong");
});
const token = jwt.sign({ email: email, password: password }, "64bits");
res.cookie("token", token);
}
});
app.get("/logout", function (req, res) {
res.cookie("token", "");
res.redirect("/");
});
app.get("/createuser", isloggedin, function (req, res) {
res.render("index");
});
app.get("/users", isloggedin, async function (req, res) {
const admin = jwt.verify(req.cookies.token, "64bits");
let adminid = await adminmodule
.findOne({ email: admin.email })
.populate("user");
res.render("users", { users: adminid.user });
});
app.post("/create", upload.single('image') , async function (req, res) {
const { name, email } = req.body;
let user = await usermodule.create({
name,
email,
image: req.file.filename
});
const admin = jwt.verify(req.cookies.token, "64bits");
const adminid = await adminmodule.findOne({ email: admin.email });
await adminid.user.push(user._id);
adminid.save();
res.redirect("/users");
});
app.get("/delete/:id", async function (req, res) {
await usermodule.findOneAndDelete({ _id: req.params.id });
res.redirect("/users");
});
app.get("/edit/:id", async function (req, res) {
let user = await usermodule.findOne({ _id: req.params.id });
res.render("edit", { user });
});
app.post("/update/:id", async function (req, res) {
const { name, email, image } = req.body;
await usermodule.findOneAndUpdate(
{ _id: req.params.id },
{ name: name, email: email, image: image }
);
res.redirect("/users");
});
function isloggedin(req, res, next) {
if (req.cookies.token === "") {
res.send("you are not authorized");
} else {
let data = jwt.verify(req.cookies.token, "64bits");
req.user = data;
next();
}
}
app.listen(3000, function () {
console.log("the server is running on port 3000");
});