-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (105 loc) · 3.12 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
const express = require("express");
const session = require("express-session");
const passport = require("passport");
const methodOverride = require("method-override");
const dotenv = require("dotenv");
const connectDB = require("./config/db");
const MongoStore = require("connect-mongo");
const noteRoutes = require("./routes/noteRoutes");
const taskRoutes = require("./routes/taskRoutes");
const linkRoutes = require("./routes/linkRoutes");
const { ensureAuth, ensureGuest } = require("./middleware/auth");
require("./config/auth");
// Load Config
dotenv.config({ path: "/.env" });
connectDB();
const app = express();
// Session remembers the signed in user
// Session middleware
// --------------------------------------------------
// MongoStore prevents you from being kicked out
// from being logged in, when ever the code is changed
// and refreshed on the browser and then save to the DB
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
})
); // the secret should be in an environment variable
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// register view engine
app.set("view engine", "ejs");
// middleware and static files
// app.use(express.static('public'));
app.use(express.static(__dirname + "/public"));
// Body parser
app.use(express.urlencoded({ extended: true }));
// Method Override
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method;
}
})
);
app.get("/", ensureGuest, (req, res) => {
res.render("index", {
title: "PlanDone - Student Productive Website ",
isAuth: req.isAuthenticated(),
activePath: req.url,
});
});
app.get("/plandone", ensureAuth, (req, res) => {
res.render("index", {
title: "PlanDone - Student Productive Website",
firstName: req.user.firstName,
displayName: req.user.displayName,
picture: req.user.image,
isAuth: req.isAuthenticated(),
activePath: req.url,
});
});
// Note Route
app.use("/notes", noteRoutes);
// Task Route
app.use("/tasks", taskRoutes);
app.get("/gpa-forecaster", (req, res) => {
res.render("forecaster", {
title: "CGPA Forecaster",
firstName: req.isAuthenticated() ? req.user.firstName : "",
displayName: req.isAuthenticated() ? req.user.displayName : "",
picture: req.isAuthenticated() ? req.user.image : "",
isAuth: req.isAuthenticated(),
activePath: req.url,
});
});
// Link Route
app.use("/links", linkRoutes);
// Auth with Google
app.get(
"/auth/google",
passport.authenticate("google", { scope: ["profile"] })
);
// Google auth Callback
app.get(
"/google/callback",
passport.authenticate("google", {
successRedirect: "/plandone",
failureRedirect: "/",
})
);
app.get("/logout", (req, res) => {
req.logOut();
req.session.destroy();
res.redirect("/");
});
// 404 page
// app.use((req, res) => {});
app.listen(process.env.PORT);