diff options
Diffstat (limited to 'js-maybe-remove/auth.js')
| -rwxr-xr-x | js-maybe-remove/auth.js | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/js-maybe-remove/auth.js b/js-maybe-remove/auth.js new file mode 100755 index 0000000..a0c667d --- /dev/null +++ b/js-maybe-remove/auth.js @@ -0,0 +1,166 @@ +var Auth = + { + userid: false, + username: false, + session: false, + loaded: false, + access: 0, + login: function () + { + d.warn("LOG IN") + var username = d.trim( $("#login-username").val() ) + var password = d.trim( $("#login-password").val() ) + var pwhash = $.md5("scanjam"+password) + if (! username || ! password) return + Main.enter = false + d.warn("LOGGING IN") + $.post(API.URL.auth.login, {'username':username, 'password': pwhash}, Auth.loginCallback) + $("#chat").hide() + }, + loginCallback: function (raw) + { + var lines = API.parse("/auth/login", raw) + if (! lines.length) return + if (lines[0] !== "OK") + { + alert(lines[0].split("\t")[1]) + return Auth.error() + } + u = lines[1].split("\t") + + Auth.userid = u[0] + Auth.username = u[1] + Auth.session = u[2] + Auth.access = u[3] + + document.cookie = "session="+Auth.session+";path=/;domain=.scannerjammer.com;max-age=1086400" + Auth.success() + }, + checkin: function () + { + d.warn("CHECK IN") + $.post(API.URL.auth.checkin, {'session':Auth.session}, Auth.checkinCallback) + }, + checkinCallback: function (raw) + { + var lines = API.parse("/auth/checkin", raw) + if (! lines.length) return + if (lines[0] !== "OK") + { + alert(lines[0].split("\t")[1]) + return Auth.error() + } + u = lines[1].split("\t") + Auth.userid = u[0] + Auth.username = u[1] + Auth.success() + }, + sneakin: function (userid,username) + { + d.warn("SNEAK IN") + $.post(API.URL.auth.sneakin, {'userid':userid,'username':username}).success(Auth.sneakinCallback) + }, + sneakinCallback: function (raw) + { + var lines = API.parse("/auth/sneakin", raw) + if (! lines.length) return + if (lines[0] !== "OK") + { + alert(lines[0].split("\t")[1]) + return Auth.error() + } + d.joy("snuck in!") + u = lines[1].split("\t") + + Auth.userid = u[0] + Auth.username = u[1] + Auth.session = u[2] + Auth.access = u[3] + + d.warn(lines[1]) + if (! Auth.session) + return + document.cookie = "session="+Auth.session+";path=/;domain=.scannerjammer.com;max-age=1086400" + Auth.success() + }, + logout: function () + { + d.warn("LOG OUT") + clearTimeout(Room.timer) + Room.unload() + Auth.userid = false + Auth.username = false + Local.set('userid', false) + Local.set('username', false) + document.cookie = "session=false;path=/;domain=.scannerjammer.com;max-age=0" + Auth.session = "" + Auth.load() + }, + error: function () + { + Auth.load() + }, + success: function () + { + d.joy("logged in as "+Auth.username) + Auth.unload() + Room.load() + }, + unload: function () + { + d.warn("AUTH UNLOAD") + $("#login").hide() + $("#loading").show() + Keyboard.enter = false + Auth.loaded = false + }, + load: function () + { + d.warn("AUTH LOAD") + $("#loading").hide() + $("#login").show() + $("#login-username").focus() + $("#login-username").keydown(Keyboard.textareaMap) + $("#login-password").keydown(Keyboard.textareaMap) + $("#login-password").val("") + $("#login-go").click(Auth.login) + Keyboard.enter = Auth.login + $("#bg").show() + Auth.loaded = true + }, + init: function () + { + d.warn("INIT AUTH") + if (document.cookie) + { + d.warn("got a cookie") + d.warn(document.cookie) + var cookies = document.cookie.split(";") + for (i in cookies) + { + var cookie = cookies[i].split("=") + if (cookie[0].indexOf("session") !== -1) + { + if (cookie[1] !== 'false' && cookie[1] !== 'undefined') + { + Auth.session = cookie[1] + break + } + } + } + d.warn("got sessionid "+Auth.session) + if (Auth.session) + return true + } + var userid = Local.get('userid') + var username = Local.get('username') + if (userid && username) + { + d.warn("attempting to sneak in "+username) + Auth.sneakin(userid,username) + return true + } + return false + } + } + |
