Make API object propagate authentication failures
authorChip Black <bytex64@bytex64.net>
Tue, 21 Oct 2014 07:21:01 +0000 (02:21 -0500)
committerChip Black <bytex64@bytex64.net>
Tue, 21 Oct 2014 07:21:28 +0000 (02:21 -0500)
Auto-logout when an API call gives a 403

www/jssrc/blerg/API.js
www/jssrc/blerg/Blerg.js

index 19b6734..64b1108 100644 (file)
@@ -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();
     }
 });
index 361b0c9..80037c4 100644 (file)
@@ -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();
     }
 });