summaryrefslogtreecommitdiff
path: root/public/js/auth.js
diff options
context:
space:
mode:
Diffstat (limited to 'public/js/auth.js')
-rw-r--r--public/js/auth.js88
1 files changed, 88 insertions, 0 deletions
diff --git a/public/js/auth.js b/public/js/auth.js
new file mode 100644
index 0000000..e16c2d2
--- /dev/null
+++ b/public/js/auth.js
@@ -0,0 +1,88 @@
+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 += "<option value='"+host.id+"' selected='1'>"+host.name+"</option>"
+ else
+ hostSelect += "<option value='"+host.id+"'>"+host.name+"</option>"
+ 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()
+ })
+ },
+ };