-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
41 lines (35 loc) · 1.12 KB
/
index.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
// index.js
const bodyParser = require("body-parser");
const session = require("express-session");
const passport = require("passport");
const passportConfig = require("./utils/passport");
const authRoutes = require("./routes/auth");
const profileRoutes = require("./routes/profile");
const indexRouter = require("./routes/index");
module.exports = function (app, options = {}) {
const { sessionSecret, usernameField = "username", passwordField = "password" } = options;
// Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Session setup
app.use(
session({
secret: sessionSecret,
resave: false,
saveUninitialized: false,
})
);
// Initialize passport
passportConfig(passport, { usernameField, passwordField });
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use("/auth", authRoutes);
app.use("/profile", profileRoutes);
app.use("/", indexRouter);
// Default error handler
app.use((err, _, res, _) => {
console.error(err.stack);
res.status(500).json({ message: "Internal Server Error" });
});
};