From b972385a838e0f54755d8e9a59bcf6ca5aeee463 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sat, 23 Feb 2013 11:46:30 -0800 Subject: logging In --- public/js/auth.js | 38 ++++++++++++++++++++++++++++++++++++++ public/js/chat.js | 4 ++++ public/js/game.js | 4 +++- public/js/util.js | 3 +++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 public/js/auth.js (limited to 'public/js') diff --git a/public/js/auth.js b/public/js/auth.js new file mode 100644 index 0000000..6cdc4c0 --- /dev/null +++ b/public/js/auth.js @@ -0,0 +1,38 @@ +var Auth = { + init: function(){ + Auth.bind(); + if (Auth.loggedIn()) { + } + else { + $("#login").show(); + } + }, + bind: function(){ + $("#login-go").click(Auth.login); + $("#username").keydown(function(e){ + switch (e.keyCode) { + case 13: // enter + Auth.login(); + break + } + }); + }, + loggedIn: function(){ + if (localStorage && localStorage.nick) { + Game.nick = strip(localStorage.nick); + if (Game.nick.length == 0) return false; + return true; + } + return false; + }, + login: function(){ + Game.nick = strip( $("#username").val() ); + if (Game.nick.length == 0) return; + localStorage.nick = Game.nick; + $("#username").val(""); + $("#login").hide(); + }, + logout: function(){ + $("#login").show(); + } +} diff --git a/public/js/chat.js b/public/js/chat.js index 67b6255..752ed67 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -2,6 +2,10 @@ var Chat = { height: 300, init: function(){ + Chat.bind(); + }, + + bind: function(){ $("#chat-message").on("keydown", Chat.keys); }, diff --git a/public/js/game.js b/public/js/game.js index 3b0a71b..af05ad9 100644 --- a/public/js/game.js +++ b/public/js/game.js @@ -1,13 +1,14 @@ var socket = io.connect(window.location.hostname); var Game = { - nick: "ryz", + nick: null, init: function(){ // Bind events from the server socket.on('event-join', Events.receive.join); socket.on('event-message', Events.receive.message); + Auth.init(); Chat.init(); } } @@ -15,6 +16,7 @@ var Game = { var Events = { receive: { + join: function(data){ for (var i in data.messages) { Chat.add(data.messages[i]); diff --git a/public/js/util.js b/public/js/util.js index 12a331a..e51b844 100644 --- a/public/js/util.js +++ b/public/js/util.js @@ -1,6 +1,9 @@ function trim (s) { return s.replace(/^\s+/, "").replace(/\s+$/, ""); } +function strip (s) { + return trim(s).replace(/\W+/, ""); +} function sanitize (s) { return s.replace(/&/g, "&").replace(//g, ">").replace(/\"/g, """); } -- cgit v1.2.3-70-g09d2