-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
134 lines (128 loc) · 4.29 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
131
132
133
134
/* eslint-disable no-undef */
const express = require("express");
const csrf = require("tiny-csrf");
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const path = require("path");
const { User } = require("./models");
const passport = require("passport");
const connectEnsureLogin = require("connect-ensure-login");
const session = require("express-session");
const LocalStrategy = require("passport-local");
const bcrypt = require("bcrypt");
const usersRoutes = require("./routes/users");
const sportsRoutes = require("./routes/sports");
const sessionsRoutes = require("./routes/sessions");
const userSessionsRoutes = require("./routes/usersessions");
const dashboardRoutes = require("./routes/dashboard");
const loginRoutes = require("./routes/login");
const loginSessionsRoutes = require("./routes/loginSession");
const signupRoutes = require("./routes/signup");
const profileRoutes = require("./routes/profile");
const sigoutRoutes = require("./routes/signout");
const playerSessionsRoutes = require("./routes/playerSessions");
const reportsRoutes = require("./routes/reports");
const flash = require("connect-flash");
app.set("views", path.join(__dirname, "views"));
app.use(flash());
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser("shh! some secret string"));
app.use(csrf("this_should_be_32_character_long", ["POST", "PUT", "DELETE"]));
app.use(express.static(path.join(__dirname, "public")));
app.set("view engine", "ejs");
app.use(
session({
secret: "my-super-secret-key-21728172615261562",
cookie: {
maxAge: 24 * 60 * 60 * 1000, //24hrs
},
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(function (request, response, next) {
response.locals.messages = request.flash();
next();
});
passport.use(
new LocalStrategy(
{
usernameField: "email",
passwordField: "password",
},
(userName, password, done) => {
if (userName.trim() === "") {
return done(null, false, { message: "Email cannot be empty" });
}
if (password.trim() === "") {
return done(null, false, { message: "Password cannot be empty" });
}
User.findOne({ where: { email: userName } })
.then(async (user) => {
if (user) {
const result = await bcrypt.compare(password, user.password);
if (result) {
return done(null, user);
} else {
return done(null, false, { message: "Invalid password" });
}
} else {
return done(null, false, {
message: "Invalid account credentials",
});
}
})
.catch((err) => {
return done(err);
});
}
)
);
passport.serializeUser((user, done) => {
console.log("serializing user in session", user.id);
done(null, user.id);
});
passport.deserializeUser((id, done) => {
User.findByPk(id)
.then((user) => {
console.log("deserializing user in session", user.id);
done(null, user);
})
.catch((err) => {
done(err, null);
});
});
const ensureNotAuthenticated = (req, res, next) => {
if (req.isAuthenticated()) {
// User is authenticated, redirect to "/dashboard" or any other appropriate page
return res.redirect("/dashboard");
}
// User is not authenticated, continue to the next middleware
return next();
};
app.get("/", ensureNotAuthenticated, (req, res) => {
res.render("index");
});
app.use("/users", usersRoutes);
app.use("/sports", connectEnsureLogin.ensureLoggedIn(), sportsRoutes);
app.use("/sessions", connectEnsureLogin.ensureLoggedIn(), sessionsRoutes);
app.use("/reports", connectEnsureLogin.ensureLoggedIn(), reportsRoutes);
app.use("/usersessions", userSessionsRoutes);
app.use("/dashboard", connectEnsureLogin.ensureLoggedIn(), dashboardRoutes);
app.use(
"/playerSessions",
connectEnsureLogin.ensureLoggedIn(),
playerSessionsRoutes
);
app.use("/login", ensureNotAuthenticated, loginRoutes);
app.use("/loginSession", loginSessionsRoutes);
app.use("/signup", ensureNotAuthenticated, signupRoutes);
app.use("/signout", sigoutRoutes);
app.use("/profile", connectEnsureLogin.ensureLoggedIn(), profileRoutes);
app.use((req, res, next) => {
res.render("./pages/404");
next();
});
module.exports = app;