diff rlgterm.js @ 127:e54018b26ed8

RLG-Web client: store login key in DOM Storage. Keep the login key in sessionStorage. This lets the user navigate away and return without needing to log in again.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 27 Aug 2012 13:43:12 -0700
parents 54979d35611a
children a613380ffdc2
line wrap: on
line diff
--- a/rlgterm.js	Sun Aug 26 19:04:57 2012 -0700
+++ b/rlgterm.js	Mon Aug 27 13:43:12 2012 -0700
@@ -82,9 +82,7 @@
 var session = {
   /* The session id assigned by the server. */
   id: null,
-  /* Login name and key */
-  lname: null,
-  lcred: null,
+  /* Login name and key are now in sessionStorage. */
   /* Whether the game is being played or just watched. */
   playing: false,
   /* WebSocket for communication */
@@ -402,7 +400,13 @@
 function setup() {
   keyHexCodes.init();
   termemu.init("termwrap", 24, 80);
-  setmode("login");
+  /* Is someone already logged in? */
+  if ("lcred" in sessionStorage) {
+    setmode("choose");
+    message("You are logged in as " + sessionStorage.getItem("lname") + ".");
+  }
+  else
+    setmode("login");
   /* Set up the text size. */
   var cssSize = termemu.view.style.fontSize;
   var match = cssSize.match(/\d*/);
@@ -463,8 +467,8 @@
     var reply = JSON.parse(req.responseText);
     if (reply.t == 'l') {
       /* Success */
-      session.lcred = reply.k;
-      session.lname = reply.u;
+      sessionStorage.setItem("lcred", reply.k);
+      sessionStorage.setItem("lname", reply.u);
       message("You are now logged in as " + reply.u + ".");
       setmode("choose");
     }
@@ -537,7 +541,7 @@
     }
     return;
   }
-  if (session.lcred) {
+  if ("lcred" in sessionStorage) {
     /* When starting the socket, the choices list might not be initialized. */
     getchoices();
   }
@@ -555,7 +559,8 @@
     if (msg.t == "t") {
       tableCurrent(msg.g);
     }
-    else if ((msg.t == "b" || msg.t == "e") && msg.p == session.lname) {
+    else if ((msg.t == "b" || msg.t == "e") && 
+             msg.p == sessionStorage.getItem("lname")) {
       getchoices();
     }
   };
@@ -578,7 +583,7 @@
   if (!statInterval) {
     statInterval = window.setInterval(getcurrent, statDelta);
   }
-  if (session.lcred)
+  if ("lcred" in sessionStorage)
     getchoices();
   var req = new XMLHttpRequest();
   req.onerror = errHandler;
@@ -603,7 +608,7 @@
 }
 
 function getchoices() {
-  if (session.id != null || !session.lcred)
+  if (session.id != null || !("lcred" in sessionStorage))
     return;
   var req = new XMLHttpRequest();
   req.onerror = errHandler;
@@ -617,8 +622,8 @@
       if (e instanceof SyntaxError)
         return;
     }
-    if (!("name" in reply) || reply["name"] != session.lname || 
-        !("stat" in reply))
+    if (!("name" in reply) || reply["name"] != sessionStorage.getItem("lname") 
+          || !("stat" in reply))
       return;
     var optdiv = document.getElementById("opttable");
     /* Don't remove the first child, it's the header. */
@@ -656,7 +661,7 @@
       optdiv.appendChild(rowdiv);
     }
   };
-  req.open('GET', '/pstatus/' + session.lname, true);
+  req.open('GET', '/pstatus/' + sessionStorage.getItem("lname"), true);
   req.send();
   return;
 }
@@ -676,14 +681,14 @@
 }
 
 function startgame(game) {
-  if (session.id != null || !session.lcred)
+  if (session.id != null || !("lcred" in sessionStorage))
     return;
   if (window.WebSocket) {
     wsStart(game);
     return;
   }
   var smsg = {};
-  smsg["key"] = session.lcred;
+  smsg["key"] = sessionStorage.getItem("lcred");
   smsg["game"] = game.uname;
   smsg["h"] = 24;
   smsg["w"] = 80;
@@ -742,9 +747,9 @@
 }
 
 function stopgame(game) {
-  if (!session.lcred)
+  if (!("lcred" in sessionStorage))
     return;
-  var stopmsg = {"key": session.lcred, "g": game.uname};
+  var stopmsg = {"key": sessionStorage.getItem("lcred"), "g": game.uname};
   var req = new XMLHttpRequest();
   req.onerror = errHandler;
   req.onreadystatechange = function () {
@@ -771,7 +776,7 @@
 
 function wsStart(game) {
   var sockurl = "ws://" + window.location.host + "/play/" + game.uname;
-  sockurl += "?key=" + session.lcred + "&w=80&h=24";
+  sockurl += "?key=" + sessionStorage.getItem("lcred") + "&w=80&h=24";
   ws = new WebSocket(sockurl);
   ws.onopen = function (event) {
     session.id = true;
@@ -884,8 +889,8 @@
     if (reply.t == 'r') {
       /* Success */
       message("Welcome " + reply.u + ", you are now registered.");
-      session.lcred = reply.k;
-      session.lname = reply.u;
+      sessionStorage.setItem("lcred", reply.k);
+      sessionStorage.setItem("lname", reply.u);
       message("You are now logged in as " + reply.u + ".");
       setmode("choose");
     }
@@ -934,7 +939,7 @@
   nsend = 0;
   nrecv = 0;
   msgQ = [];
-  if (session.lcred != null)
+  if ("lcred" in sessionStorage)
     setmode("choose");
   else
     setmode("login");
@@ -942,8 +947,8 @@
 }
 
 function logout() {
-  session.lcred = null;
-  session.lname = null;
+  sessionStorage.removeItem("lcred");
+  sessionStorage.removeItem("lname");
   setmode("login");
 }