From 95c15a0ee9d20150251e6e5331dd1008d30c9f0c Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Fri, 25 Jan 2019 15:09:29 +0100 Subject: [PATCH] #981 Login fixes --- back/methods/account/login.js | 2 +- front/core/components/watcher/watcher.js | 4 +-- front/core/services/auth.js | 26 +++----------- front/core/services/locale/en.yml | 6 ++++ front/core/services/locale/es.yml | 6 ++++ front/core/vendor.js | 2 -- front/package-lock.json | 5 --- front/package.json | 1 - front/salix/components/login/login.js | 4 +-- front/salix/locale/en.yml | 1 - front/salix/locale/es.yml | 1 - front/salix/module.js | 45 +++++++++++++++++------- 12 files changed, 54 insertions(+), 49 deletions(-) create mode 100644 front/core/services/locale/en.yml create mode 100644 front/core/services/locale/es.yml diff --git a/back/methods/account/login.js b/back/methods/account/login.js index cad85f0356..075d3669cc 100644 --- a/back/methods/account/login.js +++ b/back/methods/account/login.js @@ -46,7 +46,7 @@ module.exports = Self => { let filter = {where: {name: user}}; let instance = await Self.findOne(filter); - if (!instance || instance.password !== md5(password)) + if (!instance || instance.password !== md5(password || '')) throw err; let where = {id: instance.id}; diff --git a/front/core/components/watcher/watcher.js b/front/core/components/watcher/watcher.js index ba2cf79b4b..d00d6d782c 100644 --- a/front/core/components/watcher/watcher.js +++ b/front/core/components/watcher/watcher.js @@ -169,9 +169,9 @@ export default class Watcher extends Component { */ check() { if (this.form && this.form.$invalid) - throw new UserError(this._.instant('Some fields are invalid')); + throw new UserError('Some fields are invalid'); if (!this.dirty) - throw new UserError(this._.instant('No changes to save')); + throw new UserError('No changes to save'); } /** diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 30b199c2cf..3cdf210555 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -7,9 +7,10 @@ import UserError from 'core/lib/user-error'; * @property {Boolean} loggedIn Whether the user is currently logged */ export default class Auth { - constructor($http, $state, $transitions, $window, vnToken, vnModules, aclService) { + constructor($http, $q, $state, $transitions, $window, vnToken, vnModules, aclService) { Object.assign(this, { $http, + $q, $state, $transitions, $window, @@ -43,7 +44,7 @@ export default class Auth { } login(user, password, remember) { if (!user) - throw new UserError('Please insert your user and password'); + return this.$q.reject(new UserError('Please enter your username')); let params = { user, @@ -51,8 +52,7 @@ export default class Auth { }; return this.$http.post('/api/Accounts/login', params).then( - json => this.onLoginOk(json, remember), - json => this.onLoginErr(json)); + json => this.onLoginOk(json, remember)); } onLoginOk(json, remember) { this.vnToken.set(json.data.token, remember); @@ -65,22 +65,6 @@ export default class Auth { this.$state.go('home'); }); } - onLoginErr(json) { - let message; - - switch (json.status) { - case 401: - message = 'Invalid credentials'; - break; - case -1: - message = 'Can\'t contact with server'; - break; - default: - message = 'Something went wrong'; - } - - throw new UserError(message); - } logout() { let promise = this.$http.post('/api/Accounts/logout', null, { headers: {Authorization: this.vnToken.token} @@ -106,6 +90,6 @@ export default class Auth { }); } } -Auth.$inject = ['$http', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; +Auth.$inject = ['$http', '$q', '$state', '$transitions', '$window', 'vnToken', 'vnModules', 'aclService']; ngModule.service('vnAuth', Auth); diff --git a/front/core/services/locale/en.yml b/front/core/services/locale/en.yml new file mode 100644 index 0000000000..eab8860bef --- /dev/null +++ b/front/core/services/locale/en.yml @@ -0,0 +1,6 @@ +Invalid login: Invalid login, remember that distinction is made between uppercase and lowercase +Could not contact the server: Could not contact the server, make sure you have a connection to the network +Please enter your username: Please enter your username +It seems that the server has fall down: It seems that the server has fall down, wait a few minutes and try again +Your session has expired: Your session has expired +Access denied: Access denied \ No newline at end of file diff --git a/front/core/services/locale/es.yml b/front/core/services/locale/es.yml new file mode 100644 index 0000000000..78c9114596 --- /dev/null +++ b/front/core/services/locale/es.yml @@ -0,0 +1,6 @@ +Invalid login: Usuario o contraseña incorrectos, recuerda que se hace distinción entre mayúsculas y minúsculas +Could not contact the server: No se ha podido contactar con el servidor, asegurate de que dispones de conexión con la red +Please enter your username: Por favor introduce tu nombre de usuario +It seems that the server has fall down: Parece que el servidor se ha caído, espera unos minutos e inténtalo de nuevo +Your session has expired: Tu sesión ha expirado +Access denied: Acción no permitida \ No newline at end of file diff --git a/front/core/vendor.js b/front/core/vendor.js index ccb3d2f494..e10cd1036d 100644 --- a/front/core/vendor.js +++ b/front/core/vendor.js @@ -3,7 +3,6 @@ import '@babel/polyfill'; import * as ng from 'angular'; export {ng}; -import 'angular-cookies'; import 'angular-translate'; import 'angular-translate-loader-partial'; import '@uirouter/angularjs'; @@ -11,7 +10,6 @@ import 'mg-crud'; import 'oclazyload'; export const ngDeps = [ - 'ngCookies', 'pascalprecht.translate', 'ui.router', 'mgCrud', diff --git a/front/package-lock.json b/front/package-lock.json index 5eedeaff73..52fc8d351e 100644 --- a/front/package-lock.json +++ b/front/package-lock.json @@ -31,11 +31,6 @@ "resolved": "https://registry.npmjs.org/angular/-/angular-1.7.5.tgz", "integrity": "sha512-760183yxtGzni740IBTieNuWLtPNAoMqvmC0Z62UoU0I3nqk+VJuO3JbQAXOyvo3Oy/ZsdNQwrSTh/B0OQZjNw==" }, - "angular-cookies": { - "version": "1.7.5", - "resolved": "https://registry.npmjs.org/angular-cookies/-/angular-cookies-1.7.5.tgz", - "integrity": "sha512-/8xvvSl/Z9Vwu8ChRm+OQE3vmli8Icwl8uTYkHqD7j7cknJP9kNaf7SgsENlsLVtOqLE/I7TCFYrSx3bmSeNQA==" - }, "angular-translate": { "version": "2.18.1", "resolved": "https://registry.npmjs.org/angular-translate/-/angular-translate-2.18.1.tgz", diff --git a/front/package.json b/front/package.json index d723617f63..88a693bfde 100644 --- a/front/package.json +++ b/front/package.json @@ -11,7 +11,6 @@ "@babel/polyfill": "^7.2.5", "@uirouter/angularjs": "^1.0.20", "angular": "^1.7.5", - "angular-cookies": "^1.7.5", "angular-translate": "^2.18.1", "angular-translate-loader-partial": "^2.18.1", "flatpickr": "^4.5.2", diff --git a/front/salix/components/login/login.js b/front/salix/components/login/login.js index 49df6a7013..401d4a4d91 100644 --- a/front/salix/components/login/login.js +++ b/front/salix/components/login/login.js @@ -21,11 +21,11 @@ export default class Controller { localStorage.setItem('lastUser', this.user); this.loading = false; }) - .catch(error => { + .catch(err => { this.loading = false; this.password = ''; this.focusUser(); - throw error; + throw err; }); } focusUser() { diff --git a/front/salix/locale/en.yml b/front/salix/locale/en.yml index fbae9a7427..0391172cbd 100644 --- a/front/salix/locale/en.yml +++ b/front/salix/locale/en.yml @@ -5,7 +5,6 @@ Logout: Logout Change language: Change language Profile: Profile Data saved!: Data saved! -Can't contact with server: Can't contact with server Push on applications menu: To open a module push on applications menu Clients: Clients Modules access: Modules access \ No newline at end of file diff --git a/front/salix/locale/es.yml b/front/salix/locale/es.yml index 308d0d9372..9f86e267a3 100644 --- a/front/salix/locale/es.yml +++ b/front/salix/locale/es.yml @@ -1,5 +1,4 @@ Applications: Aplicaciones -Can't contact with server: No se pudo contactar con el servidor Change language: Cambiar idioma Client Frozen: Cliente congelado Client has debt: Cliente con riesgo diff --git a/front/salix/module.js b/front/salix/module.js index 8f3a022daa..fc734e1fba 100644 --- a/front/salix/module.js +++ b/front/salix/module.js @@ -66,10 +66,12 @@ ngModule.config(config); // Unhandled exceptions -$exceptionHandler.$inject = ['vnApp', '$window', '$state']; -function $exceptionHandler(vnApp, $window, $state) { +$exceptionHandler.$inject = ['vnApp', '$window', '$state', '$injector']; +function $exceptionHandler(vnApp, $window, $state, $injector) { return function(exception, cause) { let message; + let messageT; + let $translate = $injector.get('$translate'); if (exception.name == 'HttpError') { switch (exception.xhrStatus) { @@ -78,27 +80,44 @@ function $exceptionHandler(vnApp, $window, $state) { return; } - let data = exception.data; + switch (exception.status) { + case 401: + if ($state.current.name != 'login') { + messageT = 'Your session has expired'; + let params = {continue: $window.location.hash}; + $state.go('login', params); + } else + messageT = 'Invalid login'; + break; + case 403: + messageT = 'Access denied'; + break; + case 502: + messageT = 'It seems that the server has fall down'; + break; + case -1: + messageT = 'Could not contact the server'; + break; + } - if (data && data.error instanceof Object) - message = data.error.message; - else if (exception.status === -1) - message = `Can't contact with server`; - else - message = `${exception.status}: ${exception.statusText}`; + if (!messageT) { + let data = exception.data; - if (exception.status === 401 && $state.current.name != 'login') { - let params = {continue: $window.location.hash}; - $state.go('login', params); + if (data && data.error instanceof Object) + message = data.error.message; + else + message = `${exception.status}: ${exception.statusText}`; } } else if (exception.name == 'UserError') - message = exception.message; + messageT = exception.message; else { vnApp.showError('Ups! Something went wrong'); console.error(exception); throw exception; } + if (messageT) + message = $translate.instant(messageT); vnApp.showError(message); }; }