From 5e5fff8545ad3109369464c5b4c1f6e4e6e570bd Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 4 Apr 2018 16:13:34 +0200 Subject: [PATCH] [lib.api-client] automatic logout when invalid authentication --- webpack/lib/api-client.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/webpack/lib/api-client.js b/webpack/lib/api-client.js index 848e3bf..b1e8aa1 100644 --- a/webpack/lib/api-client.js +++ b/webpack/lib/api-client.js @@ -53,14 +53,21 @@ export default class APIClient{ this.received = true; /* Try to parse JSON */ - try{ _response = JSON.parse(_response); }catch(e){ _callback({error: -1}); } + try{ _response = JSON.parse(_response); }catch(e){ _callback({error: -2}); } + + /* If authentication failed -> logout user*/ + if( typeof _response.error === 'number' && _response.error === -1 ){ + auth.token = null; + document.location = ''; + return; + } /* Launch @_callback with response */ _callback(_response); }.bind(persist); - this.xhr_driver.onclose = function(){ !this.received && _callback({ error: -2 }); }.bind(persist); + this.xhr_driver.onclose = function(){ !this.received && _callback({ error: -3 }); }.bind(persist); return this.xhr_driver.send({