-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
179 lines (156 loc) · 5.63 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const legoData = require("./modules/legoSets");
const authData = require('./modules/auth-service');
const express = require('express');
const app = express();
const HTTP_PORT = process.env.PORT || 8080;
const path = require('path');
const clientSessions = require('client-sessions');
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'public', 'views'));
app.use(express.static('public'));
app.use(express.urlencoded({extended:true})); // middleware for urlencoded data
app.use(
clientSessions({
cookieName: 'session', // this is the object name that will be added to 'req'
secret: 'o6LjQ5EVNC28ZgK64hDELM18ScpFQr', // this should be a long un-guessable string.
duration: 2 * 60 * 1000, // duration of the session in milliseconds (2 minutes)
activeDuration: 1000 * 60, // the session will be extended by this many ms each request (1 minute)
})
);
app.use((req, res, next) => {
res.locals.session = req.session;
next();
});
function ensureLogin(req, res, next) {
if (!req.session.user) {
res.redirect('/login');
} else {
next();
}
}
app.get('/', (req, res) => {
// Remove unused 'req' variable
res.render("home");
});
app.get('/login', (req, res) => {
// Remove unused 'req' variable
res.render("login", { errorMessage : "", userName : ""});
});
app.post('/login', (req, res) => {
req.body.userAgent = req.get('User-Agent');
authData.checkUser(req.body).then((user) => {
req.session.user = {
userName: user.userName, // authenticated user's userName
email: user.email,// authenticated user's email
loginHistory: user.loginHistory// authenticated user's loginHistory
}
res.redirect('/lego/sets');
}).catch((err) => {
res.render("login", { errorMessage: err, userName : req.body.userName});
});
});
app.get('/register', (req, res) => {
// Remove unused 'req' variable
res.render("register", { errorMessage: "", userName : "", successMessage: "" });
});
app.post('/register', (req, res) => {
authData.registerUser(req.body).then(() => {
res.render("register", { errorMessage: "", userName : "", successMessage: "User created" });
}).catch((err) => {
res.render("register", { errorMessage: err, userName : req.body.userName, successMessage: "" });
})
});
app.get('/logout', ensureLogin, (req, res) => {
req.session.reset();
res.redirect('/');
});
app.get('/userHistory', ensureLogin, (req, res) => {
// Remove unused 'req' variable
res.render("userHistory")
});
app.get('/about', (req, res) => {
// Remove unused 'req' variable
res.render("about");
});
app.get('/lego/addSet', (req, res) => {
// Remove unused 'req' variable
legoData.getAllThemes().then((data) => {
res.render("addSet", { themes: data });
}).catch((error) => {
res.status(404).render("404", {message : "Unable to find requested sets."})
});
});
app.post('/lego/addSet', ensureLogin, (req, res) =>{
legoData.addSet(req.body).then(() => {
res.redirect("/lego/sets");
}).catch((err) => {
//not setting status to 500 as it causes error on cyclic
res.render("500", {message : `I'm sorry, but we have encountered the following error: ${err}`});
});
});
app.get('/lego/sets', (req, res) => {
if(req.query.theme){
legoData.getSetsByTheme(req.query.theme).then(data => {
res.render("sets", {sets : data});
}).catch(error => {
res.status(404).render("404", {message : "Unable to find requested sets."});
})
}else{
legoData.getAllSets().then(data => {
res.render("sets", {sets:data})
});
}
})
app.get('/lego/sets/:set_num', (req, res) => {
legoData.getSetByNum(req.params.set_num).then(data => {
res.render("set", {set : data});
}).catch(error => {
res.status(404).render("404", {message : "Unable to find requested set."});
});
});
app.get('/lego/editSet/:num', (req, res) => {
legoData.getSetByNum(req.params.num).then(data => {
legoData.getAllThemes().then((themeData) => {
res.render("editSet", { themes: themeData, set: data });
}).catch((err) =>{
res.status(404).render("404", { message: err });
});
}).catch(error => {
res.status(404).render("404", { message: err });
});
});
app.post('/lego/editSet', ensureLogin, (req, res) => {
legoData.editSet(req.body.set_num, req.body).then(() => {
res.redirect('/lego/sets');
}).catch((err) => {
res.render("500", { message: `I'm sorry, but we have encountered the following error: ${err}` });
});
});
app.get('/lego/deleteSet/:num', (req, res) => {
legoData.deleteSet(req.params.num).then(() =>{
res.redirect('/lego/sets');
}).catch((err) => {
res.render("500", { message: `I'm sorry, but we have encountered the following error: ${err}` });
});
});
app.post('/lego/deleteSet/:num', ensureLogin, (req, res) => {
legoData.deleteSet(req.params.num).then(() =>{
res.redirect('/lego/sets');
}).catch((err) => {
res.render("500", { message: `I'm sorry, but we have encountered the following error: ${err}` });
});
});
app.use((req, res, next) => {
// Remove unused 'req' variable
res.status(404).render("404", {message : "I'm sorry, we're unable to find what you're looking for"});
});
legoData.initialize()
.then(() => authData.initialize())
.then(() => {
app.listen(HTTP_PORT, function() {
console.log(`app listening on: ${HTTP_PORT}`);
});
})
.catch((err) => {
console.log(`unable to start server: ${err}`);
});