From 5bbf5887b9ab244e999006865a691a74fbd858b9 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 25 Apr 2024 13:15:05 +0200 Subject: [PATCH] fix(salix): refs #7272 #7272 Bug when acl not loaded --- front/core/services/auth.js | 69 +++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 753bc3fba..55a4cea7e 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,10 +7,12 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { + constructor($http, $q, vnApp, $translate, $state, $transitions, $window, vnToken, vnModules, aclService) { Object.assign(this, { $http, $q, + vnApp, + $translate, $state, $transitions, $window, @@ -39,9 +41,31 @@ export default class Auth { }; if (this.vnToken.token) { - return this.loadAcls() - .then(() => true) - .catch(redirectToLogin); + const maxRetries = 5; + let retryCount = 0; + const retryDelay = 2000; // Milisegundos (1 segundo) + + const loadAclsWithRetry = () => { + return this.loadAcls(maxRetries) + .then(() => true) + .catch(error => { + retryCount++; + if (retryCount < maxRetries) { + return new Promise(resolve => { + setTimeout(() => { + this.vnApp.showMessage(this.$translate.instant('Loading...')); + resolve(loadAclsWithRetry(maxRetries - retryCount)); + }, retryDelay); + }); + } else { + // Retry limit reached, redirect to login + return redirectToLogin(); + } + }); + }; + + // Start loading ACLs with retry + return loadAclsWithRetry(); } else return redirectToLogin(); }); @@ -113,18 +137,33 @@ export default class Auth { return promise; } - loadAcls() { - return this.aclService.load() - .then(() => { - this.loggedIn = true; - this.vnModules.reset(); - }) - .catch(err => { - this.vnToken.unset(); - throw err; - }); + loadAcls(maxRetries = 1) { + const attemptLoad = retryCount => { + return this.aclService.load() + .then(() => { + this.loggedIn = true; + this.vnModules.reset(); + }) + .catch(err => { + if (retryCount >= maxRetries) { + this.vnToken.unset(); + throw err; + } else { + // Retry after delay + return new Promise((resolve, reject) => { + reject(new Error('Error al cargar los ACLs')); + }); + } + }); + }; + + // Start loading with retry + return attemptLoad(1); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; +Auth.$inject = [ + '$http', '$q', 'vnApp', '$translate', '$state', + '$transitions', '$window', 'vnToken', 'vnModules', + 'aclService']; ngModule.service('vnAuth', Auth);