-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
136 lines (126 loc) · 3.37 KB
/
server.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
// bring in environment variables from a .env file
require("dotenv").config();
const database = require("./modules/databaseConnection.js");
const path = require("path");
database.connect();
const accounts = require("./modules/accounts.js")(database);
const child = require("./modules/child.js")(database);
// import express and morgan
const express = require("express");
const session = require("express-session");
// create an application object
const app = express();
// define a PORT variable from the environment with a default value
const PORT = process.env.PORT || 8080;
// set the view engine to ejs
app.set("view engine", "ejs");
app.set("views", [
path.join(__dirname, "views"),
path.join(__dirname, "views/user/"),
path.join(__dirname, "views/user/child/"),
]);
/////////////////////////////////////
// ALL YOUR MIDDLEWARE AND ROUTES GO HERE
//app.use(morgan("tiny")) // middleware for logging
app.use(express.urlencoded({ extended: true })); //middleware for parsing urlencoded data
app.use(express.json()); // middleware for parsing incoming json
app.use("/static", express.static("static")); // to set a folder for static file serving
app.use(
session({
name: `awfefdysef`,
secret: "t48wa7f9aw8hfieuobsyziaef",
resave: false,
saveUninitialized: false,
cookie: {
secure: false, // This will only work if you have https enabled!
maxAge: 600000, // 1 min
},
})
);
/////////////////////////////////////
// Routes
const userRoute = require("./routes/userRoute.js");
app.use("/user", userRoute);
// Server Listener
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
app.get("/", (req, res) => {
res.render("home");
});
app.get("/", (req, res) => {
res.render("login");
});
app.get("/logout", async (req, res) => {
if (req.session) {
req.session.destroy((err) => {
if (err) {
res.status(400).send("Unable to log out");
} else {
res.redirect("/login");
}
});
} else {
res.end();
}
});
app.get("/login", (req, res) => {
if (req.session.auth) {
res.redirect("/user");
return;
}
res.render("login", { error: false });
});
app.post("/login", async (req, res) => {
if (req.session.auth) {
res.redirect("/user");
return;
}
try {
var result = await accounts.login(req.body.email, req.body.password);
if (result === false) {
res.render("login", { error: true });
return;
}
req.session.auth = true;
req.session.email = req.body.email;
req.session.user_id = result;
res.redirect("./user/selectchild");
} catch (e) {
console.log(e);
res.render("login", { error: true });
return;
}
//res.redirect('/pdf/Entschuldigung-Krankheit-Male.pdf?name=' + req.body.email);
});
app.get("/register", (req, res) => {
if (req.session.auth) {
res.redirect("/user");
return;
}
res.render("register", { error: false });
});
app.post("/register", async (req, res) => {
try {
if (req.session.auth) {
res.redirect("/user");
return;
}
var success = await accounts.register(
req.body.name,
req.body.address,
req.body.city,
req.body.email,
req.body.password
);
if (!success) {
res.render("register", { error: true });
return;
}
res.redirect("login");
return;
} catch (err) {
console.log(err);
}
});
app.get("*", function (req, res) {
res.redirect("/");
});