-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (129 loc) · 3.76 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
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
require("dotenv").config();
const express = require("express");
const path = require("node:path");
const session = require("express-session");
const passport = require("passport");
const mySQLStore = require("express-mysql-session")(session);
const mysql = require("mysql2");
const knex = require("./db/knex-connection");
const { localStrategy } = require("./routes/auth");
const User = require("./model/user");
const app = express();
const menuRouter = require("./routes/menu");
const dishRouter = require("./routes/dish");
const cartRouter = require("./routes/cart");
const restaurantRouter = require("./routes/restaurant");
const syncDatabase = require("./db/sync-databases");
const viewRouter = require("./routes/views");
const fileRouter = require("./routes/file");
const orderRouter = require("./routes/order");
const userRouter = require("./routes/user");
app.use(express.static("./views/public"));
app.use(express.json());
app.use(express.urlencoded({ extended: "true", limit: "50mb" })); // 50mb to allow big forms/large data.
var options = {
host: `${process.env.DB_HOST}`,
port: `${process.env.DB_PORT}`,
user: `${process.env.DB_USER}`,
password: `${process.env.DB_PASSWORD}`,
database: `${process.env.DB_NAME}`,
};
var connection = mysql.createConnection(options);
const sessionStore = new mySQLStore(
{
checkExpirationInterval: 100000, // How frequently expired sessions will be cleared; milliseconds.
expiration: 900000, // The maximum age of a valid session; milliseconds.
createDatabaseTable: true, // Whether or not to create the sessions database table, if one does not already exist.
schema: {
tableName: "sessions",
columnNames: {
session_id: "session_id",
expires: "expires",
data: "data",
},
},
},
connection
);
app.use(
session({
secret: "cats",
resave: false,
saveUninitialized: true,
store: sessionStore,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
if (req.user) {
res.locals.currentUser = {
id: req.user.dataValues.id,
username: req.user.dataValues.username,
name: req.user.dataValues.name,
role: req.user.dataValues.role,
};
}
res.locals.query = req.query;
res.locals.url = req.originalUrl;
next();
});
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.use([
menuRouter,
dishRouter,
restaurantRouter,
viewRouter,
fileRouter,
cartRouter,
orderRouter,
userRouter,
]);
syncDatabase()
.then(() => {
app.listen(8080, () => {
console.log("Server is listening on port 8080");
});
})
.catch((error) => {
console.error("Failed to sync database:", error);
});
app.post("/signup", async (req, res, next) => {
try {
req.body.role = 1;
const user = await User.create(req.body);
if (user) return res.json(user);
return new error("user could not be created");
} catch (err) {
return next(err);
}
});
app.post(
"/login",
passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/failure",
})
);
app.post("/logout", function (req, res, next) {
req.logout(function (err) {
if (err) {
return next(err);
}
res.redirect("/");
});
});
passport.use(localStrategy);
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
// TODO: Implement redis.
const user = await User.findByPk(id);
done(null, user);
} catch (err) {
done(err);
}
});