-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
171 lines (158 loc) · 4.38 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const express = require("express");
const app = express();
const bcrypt = require("bcrypt");
const passport = require("passport");
const flash = require("express-flash");
const session = require("express-session");
const methodOverride = require("method-override");
const multer = require("multer");
const fs = require("fs");
const path = require("path");
const initializePassport = require("./passport");
initializePassport(
(username) => users.find((user) => user.username === username),
(id) => users.find((user) => user.id === id)
);
// Set The Storage Engine
const storage = multer.diskStorage({
destination: "./public/uploads/",
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
// Init Upload
const upload = multer({
storage: storage,
limits: { fileSize: 1000000 },
}).single("myFile");
let users;
fs.readFile("./db.json", (err, data) => {
if (err) console.log(err);
users = JSON.parse(data);
});
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: false }));
app.use(flash());
app.use(
session({
secret: process.env.SESSION_SECRET || "secret",
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
app.use(express.static("./public"));
// Routes
app.get("/", checkAuthenticated, (req, res) => {
let user = users.find((user) => user.username === req.user.username);
res.render("index.ejs", { user: user });
});
app.post("/", checkAuthenticated, (req, res) => {
let user = users.find((user) => user.username === req.user.username);
upload(req, res, (err) => {
if (err) {
res.render("index", {
user: user,
msg: err,
});
} else {
if (req.file == undefined) {
res.render("index", {
user: user,
msg: "Error: No File Selected!",
});
} else {
for (let i = 0; i < users.length; i++) {
if (users[i].username == req.user.username) {
users[i].files[req.query.tab].push(req.file.filename);
}
}
fs.writeFile("./db.json", JSON.stringify(users, null, "\t"), (err) => {
if (err) console.log(err);
});
res.render("index", {
user: user,
msg: "File Uploaded!",
file: `uploads/${req.file.filename}`,
});
}
}
});
});
app.get("/download/:id", checkAuthenticated, (req, res) => {
let user = users.find((user) => user.username === req.user.username);
let file =
user.files.syllabus.find((file) => file == req.params.id) || user.files.notice.find((file) => file == req.params.id) ||
user.files.invoice.find((file) => file == req.params.id);
var x = __dirname + "/public/uploads/" + file;
res.download(x);
});
app.get("/login", checkNotAuthenticated, (req, res) => {
res.render("login.ejs");
});
app.post(
"/login",
checkNotAuthenticated,
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true,
})
);
app.get("/register", checkNotAuthenticated, (req, res) => {
res.render("register.ejs");
});
app.post("/register", checkNotAuthenticated, async (req, res) => {
if (typeof users.find((user) => user.username == req.body.username) != 'undefined') {
return res.redirect("/register");
}
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
users.push({
id: Date.now().toString(),
username: req.body.username,
password: hashedPassword,
name: "",
description: "",
files: {
syllabus: [],
notice: [],
invoice: [],
},
});
fs.writeFile("./db.json", JSON.stringify(users, null, "\t"), (err) => {
if (err) console.log(err);
});
res.redirect("/login");
} catch {
res.redirect("/register");
}
});
app.delete("/logout", (req, res) => {
req.logOut();
res.redirect("/login");
});
function checkAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect("/login");
}
function checkNotAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return res.redirect("/");
}
next();
}
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running at port: ${PORT}`);
});