-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
77 lines (66 loc) · 2.54 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
import express from 'express';
import session from 'express-session';
import exphbs from 'express-handlebars';
import configRoutes from './routes/index.js';
import Handlebars from 'handlebars';
const app = express();
const staticDir = express.static('public');
app.use('/public', staticDir);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
name: 'AuthState',
secret: 'some secret string!',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use((req, res, next) => {
const timestamp = new Date().toUTCString();
const method = req.method;
const route = req.originalUrl;
const isAuthenticated = req.session.player ? "Authenticated Player" : "Non-Authenticated Player";
console.log(`[${timestamp}]: ${method} ${route} (${isAuthenticated})`);
if (!req.session.player && (route !== '/login' && route !== '/register' && route !== '/leaderboard')) {
return res.redirect('/login');
}
if (req.session.player && (route === '/login')) {
return res.redirect('/city');
}
//better way to make middleware according to w3schools
if (req.session.player && !['/city', '/logout', '/buy-building', '/destroy-building', '/get-player', '/pvp', '/report', '/report-player', '/pvp/targeted-battle', '/pvp/random-attack', '/pvp/execute-battle', '/tasks', '/help', '/options', '/leaderboard'].includes(route)) {
return res.redirect('/city');
}
next();
});
configRoutes(app);
const handlebarsInstance = exphbs.create({
defaultLayout: 'main',
helpers: {
asJSON: (obj, spacing) => {
if (typeof spacing === 'number') {
return new Handlebars.SafeString(JSON.stringify(obj, null, spacing));
}
return new Handlebars.SafeString(JSON.stringify(obj));
},
eq: (v1, v2) => v1 === v2,
isEmptyObject: (obj) => {
return Object.keys(obj).length === 0 && obj.constructor === Object;
},
floor: (u) => Math.floor(u)
}
});
//stackoverflow on how to use spaced atrribute objects in handlebars
handlebarsInstance.handlebars.registerHelper('getProperty', function(object, property){
return object[property];
});
handlebarsInstance.handlebars.registerHelper('json', function(context){
return JSON.stringify(context);
});
app.engine('handlebars', handlebarsInstance.engine);
app.set('view engine', 'handlebars');
// Listen on port 3000
app.listen(3000, () => {
console.log("We've now got a server!");
console.log('Your routes will be running on http://localhost:3000');
});