diff --git a/back/methods/vn-user/renew-token.js b/back/methods/vn-user/renew-token.js index c7214453d..1f3532bd6 100644 --- a/back/methods/vn-user/renew-token.js +++ b/back/methods/vn-user/renew-token.js @@ -1,6 +1,8 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethodCtx('renewToken', { - description: 'Send email to the user', + description: 'Checks if the token has more than renewPeriod seconds to live and if so, renews it', accepts: [], returns: { type: 'Object', @@ -22,16 +24,10 @@ module.exports = Self => { const differenceMilliseconds = now - created; const differenceSeconds = Math.floor(differenceMilliseconds / 1000); - const accessTokenConfig = await models.AccessTokenConfig.findOne(); - if (differenceSeconds <= accessTokenConfig.renewPeriod) { - const response = { - statusCode: 200, - data: { - message: 'Token is active', - } - }; - return response; - } + const accessTokenConfig = await models.AccessTokenConfig.findOne({fields: ['renewPeriod']}); + + if (differenceSeconds <= accessTokenConfig.renewPeriod) + throw new UserError(`The renew period has not been exceeded`); await Self.logout(tokenId); const user = await Self.findById(userId); diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 6d3d7f062..7f4d2c594 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,7 +7,7 @@ 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, vnTokenCreated, vnModules, aclService) { + constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { Object.assign(this, { $http, $q, @@ -15,7 +15,6 @@ export default class Auth { $transitions, $window, vnToken, - vnTokenCreated, vnModules, aclService, loggedIn: false @@ -65,8 +64,7 @@ export default class Auth { } onLoginOk(json, remember) { - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); + this.vnToken.set(json.data.token, json.data.created, remember); return this.loadAcls().then(() => { let continueHash = this.$state.params.continue; @@ -103,6 +101,6 @@ export default class Auth { }); } } -Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnTokenCreated', 'vnModules', 'aclService']; +Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; ngModule.service('vnAuth', Auth); diff --git a/front/core/services/index.js b/front/core/services/index.js index 6d4e34428..855f2fab1 100644 --- a/front/core/services/index.js +++ b/front/core/services/index.js @@ -11,5 +11,4 @@ import './report'; import './email'; import './file'; import './date'; -import './token-created'; diff --git a/front/core/services/token-created.js b/front/core/services/token-created.js deleted file mode 100644 index c8e156a5d..000000000 --- a/front/core/services/token-created.js +++ /dev/null @@ -1,34 +0,0 @@ -import ngModule from '../module'; - -/** - * Saves and loads the created for the current logged in user. - * - * @property {String} created The current login created or %null - */ -export default class created { - constructor() { - try { - this.created = sessionStorage.getItem('vnTokenCreated'); - if (!this.created) - this.created = localStorage.getItem('vnTokenCreated'); - } catch (e) {} - } - set(value, remember) { - this.unset(); - try { - if (remember) - localStorage.setItem('vnTokenCreated', value); - else - sessionStorage.setItem('vnTokenCreated', value); - } catch (e) {} - - this.created = value; - } - unset() { - localStorage.removeItem('vnTokenCreated'); - sessionStorage.removeItem('vnTokenCreated'); - this.created = null; - } -} - -ngModule.service('vnTokenCreated', created); diff --git a/front/core/services/token.js b/front/core/services/token.js index 126fbb604..c1bb5a173 100644 --- a/front/core/services/token.js +++ b/front/core/services/token.js @@ -9,25 +9,33 @@ export default class Token { constructor() { try { this.token = sessionStorage.getItem('vnToken'); - if (!this.token) + this.created = sessionStorage.getItem('vnTokenCreated'); + if (!this.token) { this.token = localStorage.getItem('vnToken'); + this.created = localStorage.getItem('vnTokenCreated'); + } } catch (e) {} } - set(value, remember) { + set(token, created, remember) { this.unset(); try { - if (remember) - localStorage.setItem('vnToken', value); - else - sessionStorage.setItem('vnToken', value); + if (remember) { + localStorage.setItem('vnToken', token); + localStorage.setItem('vnTokenCreated', created); + } else { + sessionStorage.setItem('vnToken', token); + sessionStorage.setItem('vnTokenCreated', created); + } } catch (e) {} - this.token = value; + this.token = token; + this.created = created; } unset() { localStorage.removeItem('vnToken'); sessionStorage.removeItem('vnToken'); this.token = null; + this.created = null; } } diff --git a/front/salix/components/layout/index.js b/front/salix/components/layout/index.js index e9042da25..032a68037 100644 --- a/front/salix/components/layout/index.js +++ b/front/salix/components/layout/index.js @@ -3,13 +3,9 @@ import Component from 'core/lib/component'; import './style.scss'; export class Layout extends Component { - constructor($element, $, vnModules, vnToken, vnTokenCreated) { + constructor($element, $, vnModules, vnToken) { super($element, $); this.modules = vnModules.get(); - Object.assign(this, { - vnToken, - vnTokenCreated - }); } $onInit() { @@ -50,7 +46,7 @@ export class Layout extends Component { checkTokenValidity() { const now = new Date(); - const differenceMilliseconds = now - new Date(this.vnTokenCreated.created); + const differenceMilliseconds = now - new Date(this.vnToken.created); const differenceSeconds = Math.floor(differenceMilliseconds / 1000); if (differenceSeconds > window.localStorage.renewPeriod) { @@ -60,8 +56,7 @@ export class Layout extends Component { let remember = true; if (window.sessionStorage.vnToken) remember = false; - this.vnToken.set(json.data.token, remember); - this.vnTokenCreated.set(json.data.created, remember); + this.vnToken.set(json.data.token, json.data.created, remember); } }); } @@ -71,7 +66,7 @@ export class Layout extends Component { clearInterval(this.inservalId); } } -Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken', 'vnTokenCreated']; +Layout.$inject = ['$element', '$scope', 'vnModules', 'vnToken']; ngModule.vnComponent('vnLayout', { template: require('./index.html'), diff --git a/front/salix/components/layout/index.spec.js b/front/salix/components/layout/index.spec.js index 97a6f7d71..e94ded787 100644 --- a/front/salix/components/layout/index.spec.js +++ b/front/salix/components/layout/index.spec.js @@ -61,28 +61,28 @@ describe('Component vnLayout', () => { describe('checkTokenValidity()', () => { it(`should not call renewToken and not set vnToken in the controller`, () => { localStorage.setItem('renewPeriod', 100); - controller.vnTokenCreated.created = new Date(); + controller.vnToken.created = new Date(); controller.checkTokenValidity(); expect(controller.vnToken.token).toBeNull(); }); - it(`should call renewToken and set vnToken and vnTokenCreated properties in the controller`, () => { + it(`should call renewToken and set vnToken properties in the controller`, () => { const response = { token: 999, created: new Date() }; localStorage.setItem('renewPeriod', 100); const oneHourBefore = new Date(Date.now() - (60 * 60 * 1000)); - controller.vnTokenCreated.created = oneHourBefore; + controller.vnToken.created = oneHourBefore; $httpBackend.expect('POST', `VnUsers/renewToken`).respond(response); controller.checkTokenValidity(); $httpBackend.flush(); expect(controller.vnToken.token).toBe(999); - expect(controller.vnTokenCreated.created).toEqual(response.created); + expect(controller.vnToken.created).toEqual(response.created); }); }); }); diff --git a/loopback/locale/es.json b/loopback/locale/es.json index d88a4ebc9..5df200abb 100644 --- a/loopback/locale/es.json +++ b/loopback/locale/es.json @@ -84,7 +84,7 @@ "The current ticket can't be modified": "El ticket actual no puede ser modificado", "The current claim can't be modified": "La reclamación actual no puede ser modificada", "The sales of this ticket can't be modified": "Las lineas de este ticket no pueden ser modificadas", - "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", + "The sales do not exists": "La(s) línea(s) seleccionada(s) no existe(n)", "Please select at least one sale": "Por favor selecciona al menos una linea", "All sales must belong to the same ticket": "Todas las lineas deben pertenecer al mismo ticket", "NO_ZONE_FOR_THIS_PARAMETERS": "Para este día no hay ninguna zona configurada", @@ -293,5 +293,6 @@ "comercialName": "Comercial", "Invalid NIF for VIES": "Invalid NIF for VIES", "Ticket does not exist": "Este ticket no existe", - "Ticket is already signed": "Este ticket ya ha sido firmado" + "Ticket is already signed": "Este ticket ya ha sido firmado", + "The renew period has not been exceeded": "El periodo de renovación no ha sido superado" }