From 7e766c7dba917361e1624cf04b66019b949d5eb2 Mon Sep 17 00:00:00 2001 From: Tim Mylemans Date: Wed, 30 Mar 2016 22:02:23 +0200 Subject: Improved the ngax theme to improve handling 401 API responses --- Duplicati/Server/webroot/ngax/scripts/app.js | 4 ++ .../webroot/ngax/scripts/services/AppService.js | 53 ++++++++++++++++------ .../webroot/ngax/scripts/services/BackupList.js | 2 +- .../webroot/ngax/scripts/services/DialogService.js | 14 ++++++ 4 files changed, 57 insertions(+), 16 deletions(-) (limited to 'Duplicati/Server/webroot') diff --git a/Duplicati/Server/webroot/ngax/scripts/app.js b/Duplicati/Server/webroot/ngax/scripts/app.js index ec7c115a5..414e8b430 100644 --- a/Duplicati/Server/webroot/ngax/scripts/app.js +++ b/Duplicati/Server/webroot/ngax/scripts/app.js @@ -8,6 +8,10 @@ var backupApp = angular.module( ] ); +backupApp.constant('appConfig', { + login_url: '/login.html' +}); + backupApp.config(['$routeProvider', function($routeProvider) { $routeProvider. diff --git a/Duplicati/Server/webroot/ngax/scripts/services/AppService.js b/Duplicati/Server/webroot/ngax/scripts/services/AppService.js index bcd8784bd..5932f97b1 100644 --- a/Duplicati/Server/webroot/ngax/scripts/services/AppService.js +++ b/Duplicati/Server/webroot/ngax/scripts/services/AppService.js @@ -1,4 +1,4 @@ -backupApp.service('AppService', function($http, $cookies) { +backupApp.service('AppService', function($http, $cookies, $q, DialogService, appConfig) { this.apiurl = '/api/v1'; var setupConfig = function (method, options, data) { @@ -10,41 +10,66 @@ backupApp.service('AppService', function($http, $cookies) { options.headers = options.headers || {}; if ((method == "POST" || method == "PATCH" || method == "PUT") && options.headers['Content-Type'] == null && data != null && typeof(data) != typeof('')) { - options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; - options.transformRequest = function(obj) { - var str = []; - for(var p in obj) - str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); - return str.join("&"); - }; + options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'; + options.transformRequest = function(obj) { + var str = []; + for(var p in obj) + str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); + return str.join("&"); + }; } return options; }; + + var installResponseHook = function(promise) { + var deferred = $q.defer(); + + promise.then(function successCallback(response) { + deferred.resolve(response); + }, function errorCallback(response) { + if (response.status == 401){ + DialogService.dismissAll(); + DialogService.accept('Not logged in', function () { + window.location = appConfig.login_url; + }); + return; + } + deferred.reject(response); + }); + + return deferred.promise; + }; - this.get = function(url, options) { + this.get = function(url, options) { + var rurl = this.apiurl + url; + + return installResponseHook($http.get(rurl, setupConfig('GET', options))); + }; + + this.getx = function(url, options) { var rurl = this.apiurl + url; return $http.get(rurl, setupConfig('GET', options)); }; this.patch = function(url, data, options) { var rurl = this.apiurl + url; - return $http.patch(rurl, data, setupConfig('PATCH', options, data)); + return installResponseHook($http.patch(rurl, data, setupConfig('PATCH', options, data))); }; this.post = function(url, data, options) { var rurl = this.apiurl + url; - return $http.post(rurl, data, setupConfig('POST', options, data)); + return installResponseHook($http.post(rurl, data, setupConfig('POST', options, data))); }; this.put = function(url, data, options) { var rurl = this.apiurl + url; - return $http.put(rurl, data, setupConfig('PUT', options, data)); + return installResponseHook($http.put(rurl, data, setupConfig('PUT', options, data))); }; this.delete = function(url, options) { var rurl = this.apiurl + url; - return $http.delete(rurl, setupConfig('DELETE', options)); + return installResponseHook($http.delete(rurl, setupConfig('DELETE', options))); }; @@ -67,6 +92,4 @@ backupApp.service('AppService', function($http, $cookies) { this.get_bugreport_url = function(reportid) { return this.apiurl + '/bugreport/' + reportid + '?x-xsrf-token=' + encodeURIComponent($cookies.get('xsrf-token')); }; - - }); \ No newline at end of file diff --git a/Duplicati/Server/webroot/ngax/scripts/services/BackupList.js b/Duplicati/Server/webroot/ngax/scripts/services/BackupList.js index 2428da2e4..cb1ef759b 100644 --- a/Duplicati/Server/webroot/ngax/scripts/services/BackupList.js +++ b/Duplicati/Server/webroot/ngax/scripts/services/BackupList.js @@ -1,4 +1,4 @@ -backupApp.service('BackupList', function($rootScope, $timeout, AppService, AppUtils, ServerStatus) { +backupApp.service('BackupList', function($rootScope, $timeout, AppService, AppUtils, ServerStatus, appConfig) { var list = []; var lookup = {}; diff --git a/Duplicati/Server/webroot/ngax/scripts/services/DialogService.js b/Duplicati/Server/webroot/ngax/scripts/services/DialogService.js index 24bc32129..88d270e97 100644 --- a/Duplicati/Server/webroot/ngax/scripts/services/DialogService.js +++ b/Duplicati/Server/webroot/ngax/scripts/services/DialogService.js @@ -51,6 +51,14 @@ backupApp.service('DialogService', function() { }); }; + this.accept = function(message, callback) { + return this.enqueueDialog({ + 'message': message, + 'callback': callback, + 'buttons': ['OK'] + }); + }; + this.dialog = function(title, message, buttons, callback, onshow) { return this.enqueueDialog({ 'message': message, @@ -78,4 +86,10 @@ backupApp.service('DialogService', function() { } }; + this.dismissAll = function() { + while (state.CurrentItem != null){ + this.dismissCurrent(); + } + }; + }); \ No newline at end of file -- cgit v1.2.3