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.
This commit is contained in:
John "Elwin" Edwards 2012-08-27 13:43:12 -07:00
parent 47cc4d29b6
commit 16e66858c0

View file

@ -82,9 +82,7 @@ var games = {
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,6 +400,12 @@ function vkey(c) {
function setup() {
keyHexCodes.init();
termemu.init("termwrap", 24, 80);
/* 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;
@ -463,8 +467,8 @@ function formlogin(ev) {
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 @@ function wsCurrent() {
}
return;
}
if (session.lcred) {
if ("lcred" in sessionStorage) {
/* When starting the socket, the choices list might not be initialized. */
getchoices();
}
@ -555,7 +559,8 @@ function wsCurrent() {
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 @@ function getcurrent(clear) {
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 getcurrent(clear) {
}
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 @@ function getchoices() {
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 @@ function getchoices() {
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 makeStarter(gname) {
}
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 makeStopper(gname) {
}
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 stopgame(game) {
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 @@ function formreg(ev) {
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 @@ function gameover() {
nsend = 0;
nrecv = 0;
msgQ = [];
if (session.lcred != null)
if ("lcred" in sessionStorage)
setmode("choose");
else
setmode("login");
@ -942,8 +947,8 @@ function gameover() {
}
function logout() {
session.lcred = null;
session.lname = null;
sessionStorage.removeItem("lcred");
sessionStorage.removeItem("lname");
setmode("login");
}