-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
145 lines (116 loc) · 4.18 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
135
136
137
138
139
140
141
142
143
144
145
// ℹ️ Gets access to environment variables/settings
const cors = require("cors");
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// ℹ️ Connects to the database
require("./db");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
const app = express();
//NODEMAILER
// app.use(
// cors({
// // this could be multiple domains/origins, but we will allow just our React app
// origin: ['http://localhost:3000']
// })
// );
// ℹ️ This function is getting exported from the config folder. It runs most middlewares
require("./config")(app);
// session configuration
const session = require("express-session");
// session store using mongo
const MongoStore = require("connect-mongo").default;
const mongoose = require("./db/index");
app.use(
session({
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
cookie: { maxAge: 1000 * 60 * 60 * 24 },
//Forces the session to be saved back to the session store,
// even if the session was never modified during the request.
resave: true,
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI || "mongodb://localhost/prototype",
}),
})
);
// end of session configuration
// passport configuration
const User = require("./models/User.model");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcrypt");
// we serialize only the `_id` field of the user to keep the information stored minimum
passport.serializeUser((user, done) => {
done(null, user._id);
});
// when we need the information for the user, the deserializeUser function is called with the id that we previously serialized to fetch the user from the database
passport.deserializeUser((id, done) => {
User.findById(id)
.then((dbUser) => {
done(null, dbUser);
})
.catch((err) => {
done(err);
});
});
passport.use(
new LocalStrategy((username, password, done) => {
// login
User.findOne({ username: username })
.then((userFromDB) => {
if (userFromDB === null) {
// there is no user with this username
done(null, false, { message: "Wrong Credentials" });
} else if (!bcrypt.compareSync(password, userFromDB.password)) {
// the password is not matching
done(null, false, { message: "Wrong Credentials" });
} else {
// the userFromDB should now be logged in
done(null, userFromDB);
}
})
.catch((err) => {
console.log(err);
});
})
);
app.use(passport.initialize());
app.use(passport.session());
// end of passport
// 👇 Start handling routes here
// Contrary to the views version, all routes are controled from the routes/index.js
const allRoutes = require("./routes");
app.use("/api", allRoutes);
const auth = require("./routes/auth/auth");
app.use("/api/auth", auth);
const newsletter = require("./routes/newsletter/newsletter");
app.use("/api/newsletter", newsletter);
const gallery = require("./routes/gallery/gallery");
app.use("/api/gallery/profile", gallery);
const sales = require("./routes/gallery/sales");
app.use("/api/gallery/sales", sales);
const inventory = require("./routes/gallery/inventory");
app.use("/api/gallery/inventory", inventory);
const artists = require("./routes/artists/artists");
app.use("/api/gallery/artists", artists);
const acquisitions = require("./routes/collector/acquisitions");
app.use("/api/collector/acquisitions", acquisitions);
const contact = require("./routes/contact/contact");
app.use("/api/contact", contact);
const collector = require("./routes/collector/collector");
app.use("/api/collector/profile", collector);
// app.js
const path = require("path");
app.use(express.static(path.join(__dirname, "/client/build")));
app.use((req, res) => {
// If no routes match, send them the React HTML.
res.sendFile(__dirname + "/client/build/index.html");
});
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;