commit:6d8bbc2417758f3613bf77766fc6ee3d570d8277
author:Chip Black
committer:Chip Black
date:Tue Oct 21 02:21:01 2014 -0500
parents:c28890fa3ec170540678b83c8da9c91485ac30c1
Make API object propagate authentication failures

Auto-logout when an API call gives a 403
diff --git a/www/jssrc/blerg/API.js b/www/jssrc/blerg/API.js
line changes: +43/-5
index 19b6734..64b1108
--- a/www/jssrc/blerg/API.js
+++ b/www/jssrc/blerg/API.js
@@ -76,21 +76,29 @@ enyo.kind({
         });
         req.go();
     },
+    expireClientAuthentication: function() {
+        blerg.API.loggedIn = false;
+        blerg.API.username = '';
+        enyo.setCookie('auth', '', {"Max-Age": 0});
+    },
     logout: function() {
         var req = new enyo.Ajax({
             url: baseURL + '/logout',
             method: 'POST'
         });
-        var logout_func = function(inSender, inResponse) {
-            blerg.API.loggedIn = false;
-            blerg.API.username = '';
-            enyo.setCookie('auth', '', {"Max-Age": 0});
+        var logout_func = function() {
+            this.expireClientAuthentication();
             this.bubble('onLogoutSuccessful');
-        };
+        }.bind(this);
         req.response(this, logout_func);
         req.error(this, logout_func);
         req.go();
     },
+    authenticationFailed: function() {
+        enyo.log("Authentication failed -- logging out");
+        this.expireClientAuthentication();
+        this.bubble('onAuthFailure');
+    },
     changePassword: function(oldpassword, newpassword) {
         var req = new enyo.Ajax({
             url: baseURL + '/passwd',
@@ -186,6 +194,11 @@ enyo.kind({
         req.response(this, function(inSender, inResponse) {
             this.bubble('onStatus', inResponse);
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     },
     clearStatus: function(type) {
@@ -206,6 +219,11 @@ enyo.kind({
             inResponse.type = type;
             this.bubble('onClearStatus', inResponse);
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     },
     loadFeed: function() {
@@ -221,6 +239,11 @@ enyo.kind({
                 entries: inResponse
             });
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     },
     getSubscriptionStatus: function(username) {
@@ -233,6 +256,11 @@ enyo.kind({
                 subscribed: inResponse.subscribed
             });
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     },
     subscription: function(username, v) {
@@ -250,6 +278,11 @@ enyo.kind({
                 subscribed: inResponse.status == "success" && subv
             });
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     },
     subscribe: function(username) {
@@ -279,6 +312,11 @@ enyo.kind({
                 });
             }
         });
+        req.error(this, function() {
+            if (req.xhrResponse.status == 403) {
+                this.authenticationFailed();
+            }
+        });
         req.go();
     }
 });

diff --git a/www/jssrc/blerg/Blerg.js b/www/jssrc/blerg/Blerg.js
line changes: +5/-1
index 361b0c9..80037c4
--- a/www/jssrc/blerg/Blerg.js
+++ b/www/jssrc/blerg/Blerg.js
@@ -13,7 +13,8 @@ enyo.kind({
         onSetTitle: "setTitle",
         onPostVisibility: "postVisibilityUpdate",
         onReload: "sendReload",
-        onShowChangePassword: "showChangePassword"
+        onShowChangePassword: "showChangePassword",
+        onAuthFailure: "authFailure"
     },
     components: [
         {classes: "blerg-header", components: [
@@ -99,5 +100,8 @@ enyo.kind({
     },
     showChangePassword: function() {
         this.$.passwdDialog.show();
+    },
+    authFailure: function(inSender, inEvent) {
+        this.logout();
     }
 });