-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
94 lines (81 loc) · 2.8 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
const path = require('path');
const express = require('express');
const app = express();
require('express-ws')(app);
const cors = require('cors');
const session = require('express-session');
const { loadWebconfig } = require('./lib/functions');
const webconfig = loadWebconfig();
const MongoDBStore = require('connect-mongodb-session')(session);
const log = require('./lib/logger');
require('./lib/database');
const db = require('./lib/database');
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
cors({
origin: webconfig.dashboard_url,
credentials: true,
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
})
);
const store = new MongoDBStore({
uri: webconfig.connection_uri,
databaseName: webconfig.database,
collection: 'sessions'
});
store.on('error', function (error) {
log.error(error);
});
app.use(
session({
secret: webconfig.secret,
resave: false,
saveUninitialized: true,
cookie: {
secure: webconfig.ssl
},
store: store
})
);
app.get('/', async (req, res) => {
if (!req.session.account || !req.session.account.email) return res.redirect('/auth/login');
res.redirect('/dashboard')
});
app.use(express.static(path.resolve(__dirname, './frontend/dist')));
app.use(express.static(path.resolve(__dirname, './public')));
app.use('/api', require('./router/index.js'));
app.use('/dashboard/*', async (req, res, next) => {
if (!req.session.account) return res.redirect('/auth/login');
const user = await db.getUser(req.session.account.email);
if (!user) return res.redirect('/auth/login');
if (user.approved === false) return res.redirect('/auth/login');
const characters = await db.getCharacters(user.username);
if (!characters) {
req.session.currentCharacter = null
} else {
if (req.session.currentCharacter) {
let currentCharacterUpdated = await db.getCharacter(req.session.currentCharacter.id);
currentCharacterUpdated._id = currentCharacterUpdated._id.toString()
if (JSON.stringify(req.session.currentCharacter) != JSON.stringify(currentCharacterUpdated)) {
log.debug("The currentCharacter session is different from the one in the db (They probably switched their LEO status). Setting the new session.")
req.session.currentCharacter = currentCharacterUpdated
}
}
}
next();
});
app.get('*', async (req, res) => {
const pathname = req._parsedUrl.pathname;
if (!pathname.includes('/auth')) {
if (!pathname.includes('/api')) {
if (!req.session.account || !req.session.account.email) return res.redirect('/auth/login');
const user = await db.getUser(req.session.account.email);
if (!user) return res.redirect('/auth/login');
}
}
res.sendFile(path.resolve(__dirname, './frontend/dist', 'index.html'));
});
app.listen(webconfig.port, () => {
log.web(`Server started on port ${webconfig.port}`);
});