var Auth = { name: '', userID: 1, isHost: false, loginPrompt: false, hosts: {}, init: function () { $.post(URL.auth.login, {}, Auth.loginCallback) }, unload: function () { if (Auth.loginPrompt) { Auth.loginPrompt = false $('#login').fadeOut(1000, function(){ Main.load()} ) } else { Main.load() } }, load: function () { $('#login').fadeIn(1000) $('#login-email').focus() $('#login-email').keydown(Main.kp) $('#login-password').keydown(Main.kp) $('#login-go').click( Auth.login ) Auth.loginPrompt = true Main.saveFunction = Auth.login Main.saving = false }, login: function () { if (Main.saving) return Main.saving = true warn("attempting login") var data = { username: $('#login-email').val(), password: $('#login-password').val(), } $('#login-password').val(''), $.post(URL.auth.login, data, Auth.loginCallback) }, loginCallback: function (json) { Main.saving = false if (! json || json.error) { if (! Auth.loginPrompt) Auth.load() else warn("bad login!") return } // 0 id 1 name 2 firstname 3 email 4 access var user = Auth.user = json.user var name = user.name.split(' ')[0] || user.email.split('@')[0] warn( "Logged in! Hello "+name ) Auth.userID = user.id Auth.isHost = user.access == 2 ? true : false; Auth.name = user.name Auth.firstName = name $('#profile-edit').html(Auth.firstName + "!") $('#logout').click( Auth.logout ) var hostSelect = "" Auth.hosts = {} json.hosts.forEach(function(host){ if (host.id === Auth.userID) hostSelect += "" else hostSelect += "" Auth.hosts[ host.id ] = host.name }) $("#user-host").html(hostSelect) Auth.unload() }, logout: function () { warn("logging out") $('#login-email').val(''), $.get(URL.auth.logout,{}, function(){ Main.unload() }) }, };