X-Git-Url: http://git.bytex64.net/?a=blobdiff_plain;f=www%2Fjssrc%2Fblerg%2FAPI.js;fp=www%2Fjssrc%2Fblerg%2FAPI.js;h=2fb9561464620d9f1ff7a36a0ecfe47c8421df3a;hb=be0647e6531fec4b4b50a1a0c0d4e826e5e6f16b;hp=04e7ff9e669a2cacec63ded2d260450a8d50a120;hpb=21db04ef3e4f66d7b5c4c9be2be36703b18cbcaa;p=blerg.git diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js index 04e7ff9..2fb9561 100644 --- a/www/jssrc/blerg/API.js +++ b/www/jssrc/blerg/API.js @@ -1,6 +1,59 @@ +var baseURL = ''; + enyo.kind({ name: "blerg.API", + loggedIn: false, + username: "", create: function() { this.inherited(arguments); + if (enyo.getCookie('auth') && enyo.getCookie('username')) { + this.loggedIn = true; + this.username = enyo.getCookie('username'); + // Defer the signal until everything's initialized + setTimeout(function() { + this.bubble('onLoginSuccessful', {username: this.username}); + }.bind(this), 0); + } + }, + login: function(username, password) { + var req = new enyo.Ajax({ + url: baseURL + '/login', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + if (inResponse.status == 'success') { + this.loggedIn = true; + this.username = username; + enyo.setCookie('username', username); + this.bubble('onLoginSuccessful', {username: username}); + } else { + enyo.setCookie('username', '', {"Max-Age": 0}); + this.bubble('onLoginFailed'); + } + }.bind(this)); + req.go({ + username: username, + password: password + }); + }, + logout: function() { + var req = new enyo.Ajax({ + url: baseURL + '/logout', + method: 'POST' + }); + req.response(function(inSender, inResponse) { + this.loggedIn = false; + enyo.setCookie('auth', '', {"Max-Age": 0}); + this.bubble('onLogoutSuccessful'); + }.bind(this)); + req.go({ + username: this.username + }); + enyo.setCookie('username', '', {"Max-Age": 0}); + }, + requestFeedStatus: function() { + if (!this.loggedIn) + throw new Error('Cannot request feed status when not logged in'); + // TODO } });