From 2ce780840babf8cb4f86dcb55e3aa7385d282d6e Mon Sep 17 00:00:00 2001 From: Ramiro Saenz Date: Tue, 3 Oct 2017 20:08:30 -0300 Subject: [PATCH] Improved Auth0 integration and fixed locales Fixes #964 --- app.json | 8 -------- app/ux/Auth0.js | 34 +++++++++++++++++++++++++-------- app/view/main/MainController.js | 10 ++++------ package.json | 2 ++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/app.json b/app.json index 8f2fc7b2..4909209a 100644 --- a/app.json +++ b/app.json @@ -103,14 +103,6 @@ { "path": "${framework.dir}/build/ext-all-rtl.js" }, - { - "path": "resources/js/auth0-7.1.0.min.js", - "remote": true - }, - { - "path": "resources/js/lock-10.2.2/lock.min.js", - "remote": true - }, { "path": "resources/js/GALocalStorage.js" }, diff --git a/app/ux/Auth0.js b/app/ux/Auth0.js index 12b39bf2..217a76f5 100644 --- a/app/ux/Auth0.js +++ b/app/ux/Auth0.js @@ -9,6 +9,9 @@ Ext.define('Rambox.ux.Auth0', { ,init: function() { var me = this; + var Auth0Lock = require('auth0-lock').default; + var Auth0 = require('auth0-js'); + // Auth0 Config me.lock = new Auth0Lock(auth0Cfg.clientID, auth0Cfg.domain, { autoclose: true @@ -30,7 +33,7 @@ Ext.define('Rambox.ux.Auth0', { ,language: localStorage.getItem('locale-auth0') === null ? 'en' : localStorage.getItem('locale-auth0') }); - me.auth0 = new Auth0({ clientID: auth0Cfg.clientID, domain : auth0Cfg.domain }); + me.auth0 = new Auth0.WebAuth({ clientID: auth0Cfg.clientID, domain : auth0Cfg.domain }); me.defineEvents(); } @@ -40,10 +43,15 @@ Ext.define('Rambox.ux.Auth0', { me.lock.on("authenticated", function(authResult) { me.lock.getProfile(authResult.idToken, function(err, profile) { - if (err) { - // Handle error + if ( err ) { + if ( err.error === 401 || err.error === 'Unauthorized' ) return me.renewToken(me.checkConfiguration); Ext.Msg.hide(); - return; + return Ext.Msg.show({ + title: 'Error' + ,message: 'There was an error getting the profile: ' + err.error_description + ,icon: Ext.Msg.ERROR + ,buttons: Ext.Msg.OK + }); } // Display a spinner while waiting @@ -141,8 +149,13 @@ Ext.define('Rambox.ux.Auth0', { me.lock.getProfile(localStorage.getItem('id_token'), function (err, profile) { if ( err ) { - if ( err.error === 401 ) return me.renewToken(me.restoreConfiguration); - return alert('There was an error getting the profile: ' + err.message); + if ( err.error === 401 || err.error === 'Unauthorized' ) return me.renewToken(me.checkConfiguration); + return Ext.Msg.show({ + title: 'Error' + ,message: 'There was an error getting the profile: ' + err.error_description + ,icon: Ext.Msg.ERROR + ,buttons: Ext.Msg.OK + }); } // First we remove all current services @@ -163,8 +176,13 @@ Ext.define('Rambox.ux.Auth0', { me.lock.getProfile(localStorage.getItem('id_token'), function (err, profile) { if ( err ) { - if ( err.error === 401 ) return me.renewToken(me.checkConfiguration); - return alert('There was an error getting the profile: ' + err.message); + if ( err.error === 401 || err.error === 'Unauthorized' ) return me.renewToken(me.checkConfiguration); + return Ext.Msg.show({ + title: 'Error' + ,message: 'There was an error getting the profile: ' + err.error_description + ,icon: Ext.Msg.ERROR + ,buttons: Ext.Msg.OK + }); } if ( !profile.user_metadata ) { diff --git a/app/view/main/MainController.js b/app/view/main/MainController.js index c7fe5a46..f796c68c 100644 --- a/app/view/main/MainController.js +++ b/app/view/main/MainController.js @@ -188,8 +188,10 @@ Ext.define('Rambox.view.main.MainController', { } else { Ext.cq1('app-main').suspendEvent('remove'); Ext.getStore('Services').load(); + const count = Ext.getStore('Services').getCount(); + var i = 1; Ext.Array.each(Ext.getStore('Services').collect('id'), function(serviceId) { - me.removeServiceFn(serviceId, 1, 2); + me.removeServiceFn(serviceId, count, i++); }); if ( Ext.isFunction(callback) ) callback(); Ext.cq1('app-main').resumeEvent('remove'); @@ -462,16 +464,12 @@ Ext.define('Rambox.view.main.MainController', { Ext.cq1('app-main').getViewModel().set('avatar', ''); if ( Ext.isFunction(callback) ) callback(); - - Ext.Msg.hide(); } if ( btn ) { Ext.Msg.confirm(locale['app.main[21]'], locale['app.window[38]'], function(btnId) { if ( btnId === 'yes' ) { - logoutFn(function() { - me.removeAllServices(); - }); + logoutFn(me.removeAllServices.bind(me)); } }); } else { diff --git a/package.json b/package.json index c189e972..360d9c50 100644 --- a/package.json +++ b/package.json @@ -101,6 +101,8 @@ }, "dependencies": { "@exponent/electron-cookies": "2.0.0", + "auth0-js": "^8.10.1", + "auth0-lock": "^10.22.0", "auto-launch-patched": "5.0.2", "electron-config": "0.2.1", "electron-is-dev": "^0.1.2",