-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpageUtils.js
More file actions
167 lines (148 loc) · 5.35 KB
/
pageUtils.js
File metadata and controls
167 lines (148 loc) · 5.35 KB
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
const fs = require("fs");
const pathUtils = require("path");
const Mustache = require("mustache");
class PageUtils {
constructor() {
this.errorOutput = {
JSON_ERROR_OUTPUT: 0,
PAGE_ERROR_OUTPUT: 1,
SOCKET_ERROR_OUTPUT: 2,
};
}
renderPage(res, path, options, parameters) {
const getOption = (name, defaultValue) => {
if (typeof options[name] === "undefined") {
return defaultValue;
}
return options[name];
};
const tempScriptList = getOption("scripts", []);
let tempStylesheetList = getOption("stylesheets", []);
const tempShouldDisplayTitle = getOption("shouldDisplayTitle", true);
const tempContentWidth = getOption("contentWidth", 680);
if ("stylesheets" in ostracodMultiplayer.serverConfig) {
const tempDefaultStylesheetList = ostracodMultiplayer.serverConfig.stylesheets;
tempStylesheetList = tempStylesheetList.slice();
for (const stylesheet of tempDefaultStylesheetList) {
tempStylesheetList.unshift(stylesheet);
}
}
const tempTemplate = fs.readFileSync(path, "utf8");
const tempContent = Mustache.render(tempTemplate, parameters);
res.render("template.html", {
gameName: ostracodMultiplayer.serverConfig.gameName.toUpperCase(),
scripts: tempScriptList,
stylesheets: tempStylesheetList,
shouldDisplayTitle: tempShouldDisplayTitle,
content: tempContent,
contentWidth: tempContentWidth,
});
}
getLocalViewPath(fileName) {
return pathUtils.join(ostracodMultiplayer.localViewsDirectory, fileName);
}
getConsumerViewPath(fileName) {
return pathUtils.join(ostracodMultiplayer.consumerViewsDirectory, fileName);
}
serveMessagePage(res, message, url, urlLabel) {
pageUtils.renderPage(
res,
pageUtils.getLocalViewPath("message.html"),
{},
{ message, url, urlLabel },
);
}
generateReturnUrl(req) {
if (pageUtils.isAuthenticated(req)) {
return {
url: "/menu",
urlLabel: "Return to Main Menu",
};
} else {
return {
url: "/login",
urlLabel: "Return to Login Page",
};
}
}
reportDatabaseErrorWithJson(error, req, res) {
res.json({
success: false,
message: "An error occurred. Please contact an administrator.",
});
console.log(error);
}
reportDatabaseErrorWithPage(error, req, res) {
const tempUrl = pageUtils.generateReturnUrl(req);
pageUtils.serveMessagePage(
res,
"An error occurred. Please contact an administrator.",
tempUrl.url,
tempUrl.urlLabel,
);
console.log(error);
}
reportDatabaseError(error, errorOutput, req, res) {
console.log(error);
if (errorOutput === pageUtils.errorOutput.JSON_ERROR_OUTPUT) {
pageUtils.reportDatabaseErrorWithJson(error, req, res);
}
if (errorOutput === pageUtils.errorOutput.PAGE_ERROR_OUTPUT) {
pageUtils.reportDatabaseErrorWithPage(error, req, res);
}
}
getUsername(req) {
return req.session.username;
}
isAuthenticated(req) {
if (ostracodMultiplayer.mode === "development") {
if (!req.session.username) {
const tempUsername = req.query.username;
if (tempUsername) {
req.session.username = tempUsername;
}
}
}
return (typeof req.session.username !== "undefined");
}
checkAuthentication(errorOutput) {
if (errorOutput === pageUtils.errorOutput.SOCKET_ERROR_OUTPUT) {
return (ws, req, next) => {
if (pageUtils.isAuthenticated(req)) {
next();
} else {
ws.on("message", () => {
ws.send(JSON.stringify({
success: false,
message: "You are not currently logged in.",
}));
});
}
};
} else {
return (req, res, next) => {
if (pageUtils.isAuthenticated(req)) {
next();
} else {
if (errorOutput === pageUtils.errorOutput.JSON_ERROR_OUTPUT) {
res.json({
success: false,
message: "You are not currently logged in.",
});
}
if (errorOutput === pageUtils.errorOutput.PAGE_ERROR_OUTPUT) {
pageUtils.serveMessagePage(
res,
"You must be logged in to view that page.",
"login",
"Log In",
);
}
}
};
}
}
}
const pageUtils = new PageUtils();
module.exports = { pageUtils };
const { ostracodMultiplayer } = require("./ostracodMultiplayer");