This repository has been archived by the owner on Dec 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
72 lines (58 loc) · 1.75 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
const express = require("express");
const keys = require("./config/keys");
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
const authRoutes = require("./routes/api/auth");
const user = require("./routes/api/user");
const admin = require("./routes/api/admin");
const superuser = require("./routes/api/super_user");
const passport = require("passport");
const Admin = require("./models/Admin");
const Super_User = require("./models/Super_User");
require("./db/mongoose");
const app = express();
app.use(express.json());
app.set("view engine", "ejs");
app.locals.moment = require("moment");
// to link statis files
app.use(express.static("./assets"));
app.use(
cookieSession({
name: "session",
keys: [keys.sessionSecret]
})
);
app.use(cookieParser());
require("./middleware/PassportMiddleware");
app.use(passport.initialize());
app.use(passport.session());
app.get("/", function (req, res) {
if (req.session.token == null) {
res.render("home", {
currentUser: req.user,
clientType: req.session.client
});
} else {
Super_User.findOne({email:req.user.email},(err,superuser)=>{
if(err) Error(err);
if(superuser){
res.redirect("/superuser/dashboard");
}else{
Admin.findOne({ email: req.user.email }, function (err, admin) {
if (err) Error(err);
if (admin) {
res.redirect("/admin/dashboard");
} else {
res.redirect("/user/dashboard");
}
});
}
})
}
});
app.use("/auth", authRoutes);
app.use("/user", user);
app.use("/admin", admin);
app.use("/superuser", superuser);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server up on port ${PORT} 🔥 `));