forked from cee-elle/fswd-agile-final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (79 loc) · 2.14 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
require("dotenv").config();
const express = require("express"),
session = require("express-session"),
cookieParser = require("cookie-parser"),
passport = require("./middleware/passport"),
flash = require("connect-flash"),
sassMiddleware = require("node-sass-middleware"),
helmet = require("helmet"),
app = express();
module.exports = (db, users) => {
app.use(express.static("Public"));
app.set("view engine", "ejs");
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(flash());
app.use(cookieParser());
app.use(helmet());
app.use(
sassMiddleware({
src: `${__dirname}/public/sass`,
dest: __dirname + "/public",
debug: true,
})
);
// home
const index_route = require("./Routes/index_route.js")();
app.use("/", index_route);
// admin middleware
const is_login = (req, res, next) => {
passport.authenticate("jwt", { session: false }, (err, user, info) => {
if (!user) {
console.log(info);
req.flash("msg", "Try to log in again.");
res.render("error", { msg: req.flash("msg") });
} else {
req.user = user;
next();
}
})(req, res, next);
};
const is_admin = (req, res, next) => {
if (req.user.role == "admin") {
next();
} else {
console.log("admin");
req.flash("msg", "Not authorized");
res.render("error", { msg: req.flash("msg") });
}
};
// admin page
const admin_route = require("./Routes/admin_route")(users);
app.use("/admin", is_login, is_admin, admin_route);
// auth route
const auth_route = require("./Routes/auth_route")(users);
app.use("/user", auth_route);
// secure
const secure_route = require("./Routes/secure_route")(users);
app.use("/secure", is_login, secure_route);
// edamam api route
const api_route = require("./Routes/api_route")();
app.use("/api", is_login, api_route);
//temp endpoint
app.get("/normal_hr", (req, res) => {
res.render("normal_hr");
});
app.get("/premium_hr", (req, res) => {
res.render("premium_hr");
});
return app;
};