]> git.cworth.org Git - empires-server/commitdiff
Add simple session tracking
authorCarl Worth <cworth@cworth.org>
Wed, 20 May 2020 23:56:43 +0000 (16:56 -0700)
committerCarl Worth <cworth@cworth.org>
Wed, 20 May 2020 23:56:43 +0000 (16:56 -0700)
By taking advantage of the express-session module.

Note that we're not actually using the session for anything yet, so we
haven't configured anything such as the expiration time or even which
backend should be used for storing the session data (meaning the
session data will just be stored in memory for now).

We do add the very beginnings of a configuration file here, which for
now simply has a "secret_session" key with the random data for the
session manager to use.

Also, we configure both the "resave" and the "saveUninitialized"
properties on the session object to avoid the warnings about the
deprecated values for these two properties.

.gitignore
lmno.js

index fa6e5f228430035236849b089b43a204cf60f79b..f2f8bda64d160c78c812349f14e72228f4038661 100644 (file)
@@ -1,2 +1,3 @@
 node_modules
 game.html
+lmno-config.json
diff --git a/lmno.js b/lmno.js
index 64a106bf9a492f0475c5e6a9800a306b0c5bfcdb..fbef78f208a7decfd46300b094c29ce554b22515 100644 (file)
--- a/lmno.js
+++ b/lmno.js
@@ -1,9 +1,35 @@
 const express = require("express");
 const cors = require("cors");
 const body_parser = require("body-parser");
+const session = require("express-session");
+
+try {
+  var lmno_config = require("./lmno-config.json");
+} catch (err) {
+  config_usage();
+  process.exit(1);
+}
+
+function config_usage() {
+  console.log(`Error: Refusing to run without configuration.
+
+Please create a file named lmno-config.json that looks as follows:
+
+{
+  "session_secret": "<this should be a long string of true-random characters>";
+}
+
+Note: Don't use the exact text above, but instead replace the string
+with what it describes: a long string of random characters.`);
+}
 
 const app = express();
 app.use(cors());
+app.use(session({
+  secret: lmno_config.session_secret,
+  resave: false,
+  saveUninitialized: false
+}));
 
 /* Load each of our game mini-apps. */
 var empires = require("./empires");